php - 在特定产品的 WooCommerce 购物车页面中的购物车项目名称后添加产品 ID
问题描述
我希望连接到woocommerce_cart_item_name
WooCommerce 中的过滤器,并希望仅在特定产品的名称后显示产品 ID。
我正在看这段代码:
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'].')';
}
这确实会返回带有产品 ID 的名称,但它适用于我商店中的所有产品。
只想显示指定产品的产品 ID。我很好奇我将如何去做这件事?
解决方案
jpneey 给出的答案不起作用(HTTP ERROR 500),因为:
echo
被用来代替return
所以你得到:
function filter_woocommerce_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
// The targeted product ids, multiple product IDs can be entered, separated by a comma
$targeted_ids = array( 30, 815 );
// Product ID
$product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
if ( in_array( $product_id, $targeted_ids ) ) {
return $item_name . ' (' . $product_id . ')';
}
return $item_name;
}
add_filter( 'woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3 );
推荐阅读
- javascript - 如何与 Stripe Checkout JS/HTML 集成
- javascript - 如何使用 TypeScript 向 ReactJS 功能组件传递 1 个或 2 个参数或不传递参数?
- r - 对给定 R 中的 2 个约束的某些行求和
- java - 地形绘制Java
- java - 微服务响应模型类
- linux - 如何将变量设置为命令的输出,然后稍后在 Linux 上的同一命令中使用该变量?
- powershell - 运行我的powershell脚本错误,同时逐行输入终端作品
- python - 使用sklearn计算最近邻图?
- php - 无法从照明转换为整数
- javascript - 链接页面更改不触发动画