0

I am showing SKU value with product title in cart page but right now each words SKU and product title getting linking separately.how can i show just single link rather then having linking separately.

for showing SKU value In cart page added this code in Functions.php

add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);
function add_sku_in_cart( $title, $values, $cart_item_key ) {
$sku = $values['data']->get_sku();
$url = $values['data']->get_permalink( $product->ID );
$final='<a href="'. $url .'">SKU: '. $sku .'</a>';
return $title ?  sprintf("%s  - ", $final) .$title : $final;
}       

Example Right now showing this

<a href="http://localhost/test/?product=child-product">SKU: asda121 </a> - <a href="http://localhost/test/?product=child-product">Child Product</a>

but I want it like that

<a href="http://localhost/test/?product=child-product">SKU: asda121 - Child Product</a>                               
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Asher
  • 131
  • 3
  • 10

3 Answers3

2

You should use your custom function hooked in woocommerce_cart_item_name filter hook this way, to get the same link on both Sku and item name (when sku exist):

add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $item_name, $cart_item, $cart_item_key  ) {

    $product = $cart_item['data'];
    $sku = $product->get_sku();

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

    $product_name = $product->get_name();
    $product_id = $product->get_id();
    $url = $product->get_permalink( $product_id );

    return '<a href="'. $url .'">Sku: ' . $sku . ' - ' .$product_name . '<a>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0
add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);

function add_sku_in_cart( $title, $values, $cart_item_key ) {

    $sku = $values['data']->get_sku();
    $url = $values['data']->get_permalink( $product->ID );
    $final='SKU: '. $sku . ($title ? ' - ' . $title : '');
    return '<a href="'. $url .'">' . $final . '</a>';
} 
Annapurna
  • 529
  • 1
  • 6
  • 19
-1
add_filter( 'woocommerce_cart_item_name', 'add_sku_in_cart', 20, 3);
function add_sku_in_cart(){
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$product_names = array();

foreach($items as $item => $values) { 

    // Retrieve WC_Product object from the product-id:
    $_woo_product = wc_get_product( $values['product_id'] );

    // Get SKU from the WC_Product object:
    $product_names[] = $_woo_product->get_sku(); 
 }
} 
KTriv
  • 86
  • 2
  • 1
    please don't answer with code-only snippets, explain why your answer helps the user – Luca Aug 09 '17 at 08:54
  • User wants to display SKU value in cart page i have used filter hook which is used for adding content in cart page using this line $items = $woocommerce->cart->get_cart(); we can get the product contents and using loop i can display sku value on specific location in cart page with product title. We have to add this function with hook in functions.php file so that for all products SKU value will be displayed with product title in cart page in woocommerce – KTriv Aug 09 '17 at 08:59
  • for me this code is not working. can you please recheck it? i cant see anything in cart page – Asher Aug 09 '17 at 11:15