Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

php - Add the product ID after the cart item name in Woocommerce cart page

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...