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 ONLY for a specific Product.
I’m looking at this code:
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 indeed returns the name with a product ID but it applies on all Products on my store.
Only want to display the product ID for a specified product. I’m curious how I’d go about doing this?
2
Answers
You are almost there. You can do this by applying some basic conditioning.
Note that I’ve used an
array
of product ids incase that your problem may need this function to apply on two or more product ids. Alternatively, you can just use this:The answer, given by jpneey does not work (HTTP ERROR 500) because:
echo
was used instead ofreturn
So you get: