2
votes

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?

1

1 Answers

1
votes

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