Search code examples
phpwordpresswoocommerceorderssku

Replace product name by SKU in Woocommerce admin orders pages


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


Solution

  • 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.