1

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.

Any help would be appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Sam Nait
  • 35
  • 6

2 Answers2

1

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). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello! This works really well and thanks so much for the help I appreciate it. One more thing... maybe I forgot to add when you go to view your cart there is still the quantity which you can decrease the item to zero which you can still remove items when 0 is selected. Would you know how to perhaps grey that out so they can't decrease or increase quantity? Thanks – Sam Nait Jan 04 '19 at 23:00
  • 1
    Thank you. I wasn't sure if I did that right. But I made another question and referenced this link. Thanks for your help! – Sam Nait Jan 04 '19 at 23:49
0

The below code will work as you want it : just change the category id in if clause:

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');
Tristup
  • 3,603
  • 1
  • 14
  • 26
  • When you add the item to cart then after, view your cart, there is still the quantity amount which you can decrease the number to zero. You can still remove items when 0 is selected. Would you know how to perhaps grey that out so they can't decrease or increase quantity? – Sam Nait Jan 05 '19 at 01:48