简体   繁体   English

从 WooCommerce 产品类别的购物车中隐藏“删除项目”

[英]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.

相关问题 在 WooCommerce 中为特定产品隐藏购物车中的“删除项目” - Hide “remove item” from cart for a specific product in WooCommerce 当来自特定产品类别的商品在 Woocommerce 的购物车中时禁用购物 - Disable shopping when an item from a specific product category is in cart in Woocommerce 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 - Disable other product categories for a cart item from specific category in Woocommerce Woocommerce从结帐/购物车页面中删除产品类别链接 - Woocommerce remove product category link from checkout/cart page 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 在 WooCommerce 的购物车中隐藏“删除项目”和“数量输入” - Hide "remove item" and "quantity input" from cart in WooCommerce Woocommerce-从类别中删除产品 - Woocommerce - remove product from category 如何隐藏添加到购物车按钮特定角色和产品类别 Woocommerce - How to hide add to cart button specific role and product category Woocommerce 根据Woocommerce中的产品类别将最大商品数量添加到购物车 - Max item quantity added to cart based on product category in Woocommerce 在Woocommerce中仅允许更新一个商品类别的购物车商品数量 - Allow cart item quantity update for only one product category in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM