Hide “remove item” from cart for a specific product in WooCommerce

前端 未结 2 1811
悲&欢浪女
悲&欢浪女 2021-01-13 16:05

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 a

2条回答
  •  爱一瞬间的悲伤
    2021-01-13 16:52

    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.

提交回复
热议问题