2

When receiving confirmation emails and/or invoice email, I would like the client see the SKU instead of the product name on the order detail.

I tried the code below, but it shows the SKU between brackets under the product name.

function sww_add_sku_woocommerce_emails( $output, $order ) {

    static $run = 0;

    if ( $run ) {
        return $output;
    }

    $args = array(
                'show_sku'      => true,
    );

    $run++;

    return $order->email_order_items_table( $args );
}

add_filter( 'woocommerce_email_order_items_table', 'sww_add_sku_woocommerce_emails', 10, 2 );

However I would like the SKU totally replace the product name and not appears between brackets and if possible to change the text by Product SKU [SKU code].

What is the correct code to replace the product name by the SKU in emails notifications?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

Note: The filter hook woocommerce_order_item_name is also used in emails/email-order-items.php template file (so on email notifications).

As it seems that you want to replace the product name by the SKU everywhere, you just need to use the same answer code to one of your previous questions, with a little addition.

So your code will be like (for orders and email notifications):

add_filter( 'woocommerce_order_item_name', 'display_sku_in_order_item', 20, 3 );
function display_sku_in_order_item( $item_name, $item, $is_visible ) {
    $product   = $item->get_product(); 

    if( $sku = $product->get_sku() ) {
        // On front end orders
        if( is_wc_endpoint_url() ) {
            $item_name = '<a href="'. $product->get_permalink() .'" class="product-sku">'. __( "Product ", "woocommerce") . $sku .'</a>';
        }
        // On email notifications
        else {
            $item_name = $sku;
        }
    }
    return $item_name;
}

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

Reference: Search woocommerce_order_item_name filter hook locations

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Indeed, i wanted to replace SKU instead of product title everwhere on the woocommerce website I'm building. Sorry again if I sent too much questions. However I hope that the questions and of course your answers can help other people as it was really difficult to find the correct answers on this forum and in other forums in general. Anyway, I thank you very much for your time and your knowledge. The reference you linked is interesting; is it possible to ask you if you have other references regarding the filter hooks in woocommerce as it seems to be very important to know that? Thank you again – Alexandre Deplancke Apr 30 '19 at 04:12
  • 1
    @AlexandreDeplancke On official API docs for Hooks: https://docs.woocommerce.com/wc-apidocs/hook-docs.html – LoicTheAztec Apr 30 '19 at 04:44