[英]Hide “remove item” from cart for WooCommerce product category
I would like to Hide "remove item" from cart for a specific product category in WooCommerce, just like in " Hide "remove item" from cart for a specific product in WooCommerce " answer thread, but for a specific product category.我想在 WooCommerce 中为特定产品类别从购物车中隐藏“删除项目”,就像在“在 WooCommerce 中为特定产品隐藏“从购物车中删除项目” ”回答线程一样,但适用于特定产品类别。
Any help would be appreciated.任何帮助,将不胜感激。
The following code will hide "remove item" from cart for a specific product category (that you will define in the 2nd function) :以下代码将针对特定产品类别(您将在第二个函数中定义)从购物车中隐藏“删除项目”:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
// Hiding "remove item" for specific product category
add_filter('woocommerce_cart_item_remove_link', 'filter_cart_item_remove_link', 20, 2 );
function filter_cart_item_remove_link( $button_link, $cart_item_key ){
// HERE your specific products categories
$categories = array( 'clothing' );
// 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( has_product_categories( $cart_item['product_id'], $categories ) )
$button_link = '';
return $button_link;
}
Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。
The below code will work as you want it : just change the category id in if clause:以下代码将按您的需要工作:只需更改if子句中的类别 ID:
function tristup_woocommerce_cart_item_remove_link($link){
preg_match('/data-product_id=\"(.*?)\"/', $link, $matches);
$flag=0;
if($matches)
{
$product_id=$matches[1];
$terms = get_the_terms ( $product_id, 'product_cat' );
foreach ( $terms as $term )
{
if($term->term_id=='210') // change with your category id or add more with in proper syntaxes
{
return '';
}
}
}
return $link;
}
add_filter('woocommerce_cart_item_remove_link', 'tristup_woocommerce_cart_item_remove_link');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.