Search code examples
phpwordpresswoocommercehook-woocommercewoocommerce-theming

Adding product attribute column to edit order page in Woocommerce


Any help with this greatly appreciated.

Tried a number of things so far to no avail, including the suggestions here:
Add product short description to Woocommerce admin orders preview

This is what I have so far:

add_action( 'woocommerce_admin_order_item_headers', 'custom_admin_order_items_headers', 20, 1 );
function custom_admin_order_items_headers( $order ){

    echo '<th>';
    echo __('Colour', 'woocommerce') . '</th>';
}

add_action( 'woocommerce_admin_order_item_values', 'custom_admin_order_item_values', 20, 3 );
function custom_admin_order_item_values( $_product, $item, $item_id ) {
             
    $colour = $_product->get_attribute( 'colour' );
    echo '<td>' . $colour . '</td>';
}

Every product has a number of attributes, and I want to display one of these attributes in a new column on the edit order page.

The problem is the product attribute is empty for some reason - if I just echo $_product, there is no data in the attribute even though there definitely is a value for it saved in the DB.

Any ideas?

I've attached a screenshot of what I would like to achieve, and while I can get the header to display fine, and even an array of data attached to the product, the actual attribute is empty. If I use:

$_product->get_attribute( 'colour' )

like I do everywhere else on the site successfully, i get an error:

Uncaught Error: Call to a member function get_attribute() on null

I tried setting global $product; in the function, nada.

enter image description here

Error log:

Fatal error: Uncaught Error: Call to a member function get_attribute() on null in /home/customer/www/mysite/public_html/wp-content/themes/mytheme/functions.php:1237 Stack trace: #0 /home/customer/www/mysite/public_html/wp-includes/class-wp-hook.php(288): custom_admin_order_item_values(NULL, Object(WC_Order_Item_Shipping), 26) #1 /home/mysite/public_html/wp-includes/class-wp-hook.php(312): WP_Hook->apply_filters('', Array) #2 /home/customer/www/mysite/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #3 /home/customer/www/mysite/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-shipping.php(53): do_action('woocommerce_adm...', NULL, Object(WC_Order_Item_Shipping), 26) #4 /home/customer/www/mysite/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-items.php(73): include('/home/customer/...') #5 /home/customer/www/mysite in /home/customer/www/mysite/public_html/wp-content/themes/mytheme/functions.php on line 1237

Line on 1237 of functions.php is:

$colour = $_product->get_attribute( 'colour' );

As stated above.


Solution

  • This worked for me:

    add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
    function my_woocommerce_admin_order_item_values($_product, $item, $item_id = null) {
    
        $colour = get_the_terms( $_product->post->ID, 'pa_colour');
        echo '<td>' . $colour[0]->name . '</td>';
    }
    

    Had to go down the route of taxonomy and not get_attribute(). Thanks to this post:

    show product meta in order items table in Order Details