5

I'm using woocommerce for my website, I want to hide "remove this item/product" for one particular item, can you please tell me how can I do so.

I would greatly appreciate any help towards this.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Shahala Anjum
  • 69
  • 1
  • 9

2 Answers2

8

Updated (for multiple product Ids in an array)

Here is the correct way to disable/remove the "Remove this item" button, for a specific product ID (that you should define) in the code below:

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;
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your replay, however I want to hide the remove item for number of products, so when I tried to alter the code accordingly with array as follows it's not working, could you please look into this: $targeted_product_ids = array ( 25, 22 ); if( $cart_item['data']->get_id() == $targeted_product_ids ) Thanks – Shahala Anjum Feb 04 '18 at 14:50
0

You can use a hook to tie into the link action and remove it for specific SKUs, something like the following, though untested it should work.

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');

You could also just use strpos on the SKU but it would possibly trigger on other pats of the URL. You can also change the data-product_sku to data-product_id to search by ID instead.

Chris Morris
  • 943
  • 2
  • 17
  • 41