在特定产品的 WooCommerce 购物车页面中的购物车项目名称后添加产品 ID
Add the product ID after the cart item name in WooCommerce cart page for specific products
我想连接到 WooCommerce 中的 woocommerce_cart_item_name
过滤器,并希望仅在特定产品的名称后显示产品 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'].')';
}
这确实是 returns 带有产品 ID 的名称,但它适用于我商店中的所有产品。
只想显示指定产品的产品 ID。我很好奇我会怎么做?
你快到了。您可以通过应用一些 基本调节 .
来做到这一点
add_filter( 'woocommerce_cart_item_name', 'just_a_test', 10, 3 );
function just_a_test( $item_name, $cart_item, $cart_item_key ) {
$product_ids = array(68, 421);
if(in_array($cart_item['product_id'], $product_ids) {
echo $item_name.' ('.$cart_item['product_id'].')';
} else {
echo $item_name;
}
}
请注意,我使用了 array
个产品 ID,以防您的问题可能需要此功能应用于两个或更多产品 ID。或者,你可以只使用这个:
//single product id
$product_id = 68;
echo $item_name . ($cart_item['product_id'] == $product_id) ? ' ('.$cart_item['product_id'].')' : '';
jpneey 给出的答案无效(HTTP 错误 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 );
我想连接到 WooCommerce 中的 woocommerce_cart_item_name
过滤器,并希望仅在特定产品的名称后显示产品 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'].')';
}
这确实是 returns 带有产品 ID 的名称,但它适用于我商店中的所有产品。
只想显示指定产品的产品 ID。我很好奇我会怎么做?
你快到了。您可以通过应用一些 基本调节 .
来做到这一点add_filter( 'woocommerce_cart_item_name', 'just_a_test', 10, 3 );
function just_a_test( $item_name, $cart_item, $cart_item_key ) {
$product_ids = array(68, 421);
if(in_array($cart_item['product_id'], $product_ids) {
echo $item_name.' ('.$cart_item['product_id'].')';
} else {
echo $item_name;
}
}
请注意,我使用了 array
个产品 ID,以防您的问题可能需要此功能应用于两个或更多产品 ID。或者,你可以只使用这个:
//single product id
$product_id = 68;
echo $item_name . ($cart_item['product_id'] == $product_id) ? ' ('.$cart_item['product_id'].')' : '';
jpneey 给出的答案无效(HTTP 错误 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 );