简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM