Replace the product name by the Sku in My account view order pages

你。 提交于 2019-12-23 02:18:39

问题


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.



来源:https://stackoverflow.com/questions/55888642/replace-the-product-name-by-the-sku-in-my-account-view-order-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!