Add the sku to order items on my account order view pages in Woocommerce

1.7k views Asked by At

In Woocommerce, I am customizing my view order in MyAccount. I already added the Product images with this answer code: Add the product image to Woocommerce my account order view

Now I would like to add the Product SKU to The View order pages but, I don't know how to get it.

Anyone have an Idea?

1

There are 1 answers

0
LoicTheAztec On

Replacing your code with the following to display the product SKU in order items:

// Display the product thumbnail in order view pages
add_filter( 'woocommerce_order_item_name', 'display_product_image_in_order_item', 20, 3 );
function display_product_image_in_order_item( $item_name, $item, $is_visible ) {
    // Targeting view order pages only
    if( is_wc_endpoint_url( 'view-order' ) ) {
        $product   = $item->get_product(); // Get the WC_Product object (from order item)
        $thumbnail = $product->get_image(array( 36, 36)); // Get the product thumbnail (from product object)
        // The thumbnail
        if( $product->get_image_id() > 0 )
            $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
        // The SKU
        if( $sku = $product->get_sku() )
            $item_name .= '<br><div class="product-sku">' . $sku . '</div>';
    }
    return $item_name;
}

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