在 WooCommerce 中针对特定产品从购物车中隐藏 "remove item"
Hide "remove item" from cart for a specific product in WooCommerce
我正在为我的网站使用 woocommerce,我想隐藏 "remove this item/product" 一个特定的项目,你能告诉我我该怎么做吗?
如果对此有任何帮助,我将不胜感激。
您可以使用挂钩绑定到 link 操作并针对特定 SKU 将其删除,如下所示,尽管未经测试它应该可以工作。
function my_woocommerce_cart_item_remove_link($link){
preg_match('/data-product_sku=\"(.*?)\"/', $link, $matches);
if($matches[1] == 'PRODUCT_SKU_TO_TRIGGER')
return '';
return $link;
}
add_filter('woocommerce_cart_item_remove_link', 'my_woocommerce_cart_item_remove_link');
您也可以只在 SKU 上使用 strpos
,但它可能会触发 URL 的其他拍打。您也可以将 data-product_sku 更改为 data-product_id 以改为按 ID 搜索。
已更新(对于数组中的多个产品 ID)
下面是 disable/remove "Remove this item" 按钮的正确方法,对于特定的产品 ID(您应该在下面的代码中定义):
add_filter('woocommerce_cart_item_remove_link', 'customized_cart_item_remove_link', 20, 2 );
function customized_cart_item_remove_link( $button_link, $cart_item_key ){
//SET HERE your specific products IDs
$targeted_products_ids = array( 25, 22 );
// Get the current cart item
$cart_item = WC()->cart->get_cart()[$cart_item_key];
// If the targeted product is in cart we remove the button link
if( in_array($cart_item['data']->get_id(), $targeted_products_ids) )
$button_link = '';
return $button_link;
}
代码进入活动子主题(或活动主题)的 function.php 文件。
已测试并有效。
我正在为我的网站使用 woocommerce,我想隐藏 "remove this item/product" 一个特定的项目,你能告诉我我该怎么做吗?
如果对此有任何帮助,我将不胜感激。
您可以使用挂钩绑定到 link 操作并针对特定 SKU 将其删除,如下所示,尽管未经测试它应该可以工作。
function my_woocommerce_cart_item_remove_link($link){
preg_match('/data-product_sku=\"(.*?)\"/', $link, $matches);
if($matches[1] == 'PRODUCT_SKU_TO_TRIGGER')
return '';
return $link;
}
add_filter('woocommerce_cart_item_remove_link', 'my_woocommerce_cart_item_remove_link');
您也可以只在 SKU 上使用 strpos
,但它可能会触发 URL 的其他拍打。您也可以将 data-product_sku 更改为 data-product_id 以改为按 ID 搜索。
已更新(对于数组中的多个产品 ID)
下面是 disable/remove "Remove this item" 按钮的正确方法,对于特定的产品 ID(您应该在下面的代码中定义):
add_filter('woocommerce_cart_item_remove_link', 'customized_cart_item_remove_link', 20, 2 );
function customized_cart_item_remove_link( $button_link, $cart_item_key ){
//SET HERE your specific products IDs
$targeted_products_ids = array( 25, 22 );
// Get the current cart item
$cart_item = WC()->cart->get_cart()[$cart_item_key];
// If the targeted product is in cart we remove the button link
if( in_array($cart_item['data']->get_id(), $targeted_products_ids) )
$button_link = '';
return $button_link;
}
代码进入活动子主题(或活动主题)的 function.php 文件。
已测试并有效。