0

Based on Adding product attribute column to edit order page in Woocommerce answer code I wrote this function:

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

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

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) {

    $marchio = get_the_terms( $_product->post->ID, 'pa_marchio');
    echo '<td>' . $marchio[0]->name . '</td>';
}

It works for simple products but not with variable products, how can fix it?


enter image description here

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Leonardo
  • 1
  • 1

1 Answers1

0

To get product ID use

$item->get_product_id()

Your function should look like this

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) {

    $marchio = get_the_terms( $item->get_product_id(), 'pa_marchio');
    echo '<td>' . $marchio[0]->name . '</td>';

}

Check this post for more information how to access orders Accessing Order Items protected data in Woocommerce 3

Snuffy
  • 1,723
  • 1
  • 5
  • 9
  • Works but now in order admin panel there are a error and I can't edit the order or other thinks: Uncaught Error: Call to undefined method WC_Order_Item_Shipping::get_product_id() in /home2/qerbellt/public_html/wp-content/themes/woodmart-child/functions.php:39 – Leonardo Sep 16 '21 at 14:13
  • Stack trace: #0 /home2/qerbellt/public_html/wp-includes/class-wp-hook.php(303): my_woocommerce_admin_order_item_values(NULL, Object(WC_Order_Item_Shipping), 6) #1 /home2/qerbellt/public_html/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters('', Array) #2 /home2/qerbellt/public_html/wp-includes/plugin.php(470): WP_Hook->do_action(Array) – Leonardo Sep 16 '21 at 14:14
  • #3 /home2/qerbellt/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-shipping.php(58): do_action('woocommerce_adm...', NULL, Object(WC_Order_Item_Shipping), 6) #4 /home2/qerbellt/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-items.php(81): include('/home2/qerbellt...') #5 /home2/qerbellt/public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-items.php(41): include('/home2/qerbellt...') #6 /home2/q – Leonardo Sep 16 '21 at 14:14