Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
12
Indexable
Never
/**
 * Add tracking code column to customer's order page
 */
function add_tracking_code_column($columns) {
    $columns['tracking_code'] = __('Tracking Code', 'flatsome-child');
    return $columns;
}
add_filter('woocommerce_my_account_my_orders_columns', 'add_tracking_code_column');

/**
 * Populate tracking code column with data
 */
function populate_tracking_code_column($order) {
    $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;
    $order_obj = wc_get_order($order_id);
    $order_notes = $order_obj->get_customer_order_notes();

    foreach ($order_notes as $note) {
        if (strpos($note->content, 'Tracking code:') !== false) {
            $tracking_code = substr($note->content, strpos($note->content, 'Tracking code:') + 14);
            $tracking_code = trim($tracking_code);
            echo $tracking_code;
            return;
        }
    }
    
    echo '-';
}
add_action('woocommerce_my_account_my_orders_column_tracking_code', 'populate_tracking_code_column', 10, 1);