最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding product SKU before cart item name in WooCommerce

programmeradmin1浏览0评论

I have this snippet that adds product SKU after cart item name in cart, mini-cart and checkout:

function show_sku_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
    // The WC_Product object
    $product = $cart_item['data'];

    // Get the  SKU
    $sku = $product->get_sku();

    // When sku doesn't exist
    if ( empty( $sku ) ) 
        return $item_name;

    // Add the sku

    if ( is_cart() ) {
         $item_name .= '<br><small class="product-sku">' . '<span class="sku-title">' . __( "SKU: ", "woocommerce") . '</span>' . $sku . '</small>';
    } else {
        $item_name .= '<small class="product-sku">' . $sku . '</small>';
    }

    return $item_name;
}

add_filter( 'woocommerce_cart_item_name', 'show_sku_in_cart_items', 99, 3 );

But I need to add this before cart item name.

I have this snippet that adds product SKU after cart item name in cart, mini-cart and checkout:

function show_sku_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
    // The WC_Product object
    $product = $cart_item['data'];

    // Get the  SKU
    $sku = $product->get_sku();

    // When sku doesn't exist
    if ( empty( $sku ) ) 
        return $item_name;

    // Add the sku

    if ( is_cart() ) {
         $item_name .= '<br><small class="product-sku">' . '<span class="sku-title">' . __( "SKU: ", "woocommerce") . '</span>' . $sku . '</small>';
    } else {
        $item_name .= '<small class="product-sku">' . $sku . '</small>';
    }

    return $item_name;
}

add_filter( 'woocommerce_cart_item_name', 'show_sku_in_cart_items', 99, 3 );

But I need to add this before cart item name.

Share Improve this question edited Sep 19, 2019 at 13:59 LoicTheAztec 3,39117 silver badges24 bronze badges asked Sep 19, 2019 at 13:29 JurajJuraj 1542 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You just need to change a bit your code this way, to get the SKU before the product name:

function show_sku_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
    // The WC_Product object
    $product = $cart_item['data'];

    // Get the  SKU
    $sku = $product->get_sku();

    // When SKU doesn't exist
    if ( empty( $sku ) ) 
        return $item_name;

    // Add SKU before
    if ( is_cart() ) {
        $item_name = '<small class="product-sku">' . '<span class="sku-title">' . __( "SKU: ", "woocommerce") . '</span>' . $sku . '</small><br>' . $item_name;
    } else {
        $item_name = '<small class="product-sku">' . $sku . '</small>' . $item_name;
    }

    return $item_name;
}

add_filter( 'woocommerce_cart_item_name', 'show_sku_in_cart_items', 99, 3 );
发布评论

评论列表(0)

  1. 暂无评论