4
votes

I'm looking to hook into the 'woocommerce_cart_item_name' filter in woocommerce and would like to display the product ID after the name. I'm working with this so far...

add_filter( 'woocommerce_cart_item_name', 'justatest' );

function justatest( $productname ) {
    echo $productname;
    // ideally echo name and product id here instead
}

This returns the name with a link around it but I want to add the actual ID of the product after the item name.

How can I add the product ID after the cart item name in Woocommerce cart page?

I know I'd need to not return first since that will pull me out of the function, but I'm curious how I'd go about doing this.

2

2 Answers

6
votes

There are some missing arguments in your hooked function and you should need to make some changes to get the product Id this way:

add_filter( 'woocommerce_cart_item_name', 'just_a_test', 10, 3 );
function just_a_test( $item_name,  $cart_item,  $cart_item_key ) {
    // Display name and product id here instead
    echo $item_name.' ('.$cart_item['product_id'].')';
}

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

Tested and works.

0
votes

Try this one,

override the following page template of woocommerce,

woocommerce/cart/cart.php in your theme

you will find a table HTML/code in it.

<td class="product-name" data-title="<?php esc_attr_e('Product', 'woocommerce'); ?>"
<?php
if (!$product_permalink) {
   echo apply_filters('woocommerce_cart_item_name', $_product->get_name(). $product_id, $cart_item, $cart_item_key) . '&nbsp;';
} else {
   echo apply_filters('woocommerce_cart_item_name', sprintf('<a href="%s">%s</a>', esc_url($product_permalink), $_product->get_name() . $product_id), $cart_item, $cart_item_key);
}

// Meta data
echo WC()->cart->get_item_data($cart_item);

// Backorder notification
if ($_product->backorders_require_notification() && $_product->is_on_backorder($cart_item['quantity'])) {
   echo '<p class="backorder_notification">' . esc_html__('Available on backorder', 'woocommerce') . '</p>';
}
?>
</td>

add this Part in cart.php

<td class="product-name">...</td>

Hope this will helps you.