在 Woocommerce 购物车页面中的购物车项目名称后添加产品 ID [英] Add the product ID after the cart item name in Woocommerce cart page
问题描述
我希望连接到 woocommerce 中的woocommerce_cart_item_name"过滤器,并希望在名称后显示产品 ID.到目前为止,我正在处理这个......
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
}
这将返回带有链接的名称,但我想在项目名称后添加产品的实际 ID.
This returns the name with a link around it but I want to add the actual ID of the product after the item name.
如何在 Woocommerce 购物车页面中的购物车项目名称后添加产品 ID?
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.
推荐答案
您的挂钩函数中缺少一些参数,您应该进行一些更改以通过这种方式获取产品 ID:
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'].')';
}
此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
经过测试并有效.
这篇关于在 Woocommerce 购物车页面中的购物车项目名称后添加产品 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!