1

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.

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 );
1
  • Wow, it was very simple. Dummy question from me :) Thank you! – Juraj Sep 19 '19 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.