1

Using Woocommerce, I would like to add SKU instead of the product name in the order view page of My Account /my-account/view-order/[orderid]/

I am able to add the SKU with a link using the code below. The SKU is displayed just after the product name on the same line.

add_filter( 'woocommerce_order_item_name', 'display_sku_in_order_item', 20, 3 );
function display_sku_in_order_item( $item_name, $item, $is_visible ) {
    if( is_wc_endpoint_url( 'view-order' ) ) {
        $product   = $item->get_product(); 
        if( $sku = $product->get_sku() )
            $item_name .= '<a href="' . $product->get_permalink() . '" class="product-sku">' . __( "Product ", "woocommerce") . $sku . '</a>';
    }
    return $item_name;
}

However, I would like to remove the product name but I don't know the code to write to do this. Any help please?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

You just need to replace $item_name .= by $item_name = in your code, like:

add_filter( 'woocommerce_order_item_name', 'display_sku_in_order_item', 20, 3 );
function display_sku_in_order_item( $item_name, $item, $is_visible ) {
    if( is_wc_endpoint_url( 'view-order' ) ) {
        $product   = $item->get_product(); 
        if( $sku = $product->get_sku() )
            $item_name = '<a href="' . $product->get_permalink() . '" class="product-sku">' . __( "Product ", "woocommerce") . $sku . '</a>';
    }
    return $item_name;
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • It works perfectly. Thanks a lot. So just the little . before the = in $item_name change everything. Can I ask you what is the difference between `$item_name .=` and `$item_name =` ? – Alexandre Deplancke Apr 28 '19 at 09:54
  • 1
    @AlexandreDeplancke In `$item_name .= 'something'` "something" is appended after the item name value… In `$item_name = 'something'` "something" replace (or become) the item name value… – LoicTheAztec Apr 28 '19 at 10:24