Replace the product name by the Sku in My account

2019-05-31 15:11发布

问题:

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?

回答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.