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 badges1 Answer
Reset to default 2You 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 );