2

Again, and it will be my last question on this topic (I apologize if I asked too much questions), I would like to replace the Product name by the SKU only in Woocommerce --> Orders --> Order Details in the backend admin of WordPress.

sku instead of product name

I didn't find any understandable explanation about how to do this on forums (I have really no skills with php code). I just found that the filter hooks that may be should be used is 'woocommerce_order_items' and 'woocommerce_order_itemmeta' but frankly speaking I don't understand how to use them.

Is it possible to know the correct code to replace the Product name by the SKU only in Woocommerce --> Orders --> Order Details in the backend admin of WordPress? If it is possible to have also some reference to understand about these hooks, I will really appreciate. Thank you

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

You need to use the composite filter hook made from WC_Order_Item get_name() method like:

add_filter( 'woocommerce_order_item_get_name', 'filter_order_item_get_name', 10, 2 );
function filter_order_item_get_name( $item_name, $order_item ) {
    if ( is_admin() && $order_item->is_type('line_item') ) {
        $product = $order_item->get_product();

        if( $sku = $product->get_sku() ) {
            $item_name = $sku;
        }
    }
    return $item_name;
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • As usual, your answer is perfect. Thank you very much. I do not understand what is the difference between a filter hook and a composite filter hook. Guess I should read articles about it. – Alexandre Deplancke Apr 30 '19 at 07:47
  • Last question: what about sku in shipping item? [sku in shipping item](https://siamstylez.com/wp-content/uploads/2019/04/screenshot-siamstylez.com-2019.04.30-15-00-24.png) Is it the code `add_filter( 'woocommerce_order_shipping_item_get_name', 'filter_order_shipping_item_get_name', 10, 2 ); function filter_order_shipping_item_get_name( $item_name, $order_item ) { if ( is_admin() && $order_item->is_type('line_item') ) { $product = $order_item->get_product(); if( $sku = $product->get_sku() ) { $item_name = $sku; } } return $item_name; }` – Alexandre Deplancke Apr 30 '19 at 08:05
  • @AlexandreDeplancke Sorry I don't catch… If you have another question, ask a new question please. – LoicTheAztec Apr 30 '19 at 16:28