Search code examples
phpwordpresswoocommerceproductcart

Disallow to remove a product with a specific ID from WooCommerce cart if another product is added


I have to implement the following logic to my Cart / Checkout pages:

If the product with ID="100" is in the Cart I need to disallow its removal from Cart if the Product with ID="101" was also added.

In simple words, the ability to remove a product is only present if a product with a specific ID was not added.

Earlier I used the following solution to disallow product removal only for specific product IDs. But this code doesn't work with variable products and I can't get how to implement my logic inside it.

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 ){
    $targeted_products_ids = array( 98,99,100 );

    $cart_item = WC()->cart->get_cart()[$cart_item_key];

    if( in_array($cart_item['data']->get_id(), $targeted_products_ids) )
        $button_link = '';

    return $button_link;
}

Thank you in advance for any help.


Solution

  • This answer allows you if product_id_1 is in the cart it will not be removable from the cart if product_id_2 is also added.

    This can be applied to multiple products via the $settings array, so a certain product cannot be removed if the corresponding product ID is in the cart.

    For clarity, this works based on the product ID (simple) or the parent ID (variable) products. If you want to apply it for variations of variable products you don't have to use get_parent_id().

    So you get:

    function filter_woocommerce_cart_item_remove_link( $link, $cart_item_key ) {
        // Settings (multiple settings arrays can be added/removed if desired)
        $settings = array(
            array(
                'product_id_1'  => 100,
                'product_id_2'  => 101,
            ),
            array(
                'product_id_1'  => 30,
                'product_id_2'  => 813,
            ),
            array(
                'product_id_1'  => 53,
                'product_id_2'  => 817,
            ),
        );
        
        // Get cart
        $cart = WC()->cart;
        
        // If cart
        if ( $cart ) {
            // Get cart item
            $cart_item = $cart->get_cart()[$cart_item_key];
            
            // Get parent/real ID
            $product_id = $cart_item['data']->get_parent_id() != 0 ? $cart_item['data']->get_parent_id() : $cart_item['data']->get_id();
            
            // Loop trough settings array
            foreach ( $settings as $key => $setting ) {
                // Compare, get the correct setting array
                if ( $product_id == $settings[$key]['product_id_1'] ) {
                    // Cart id of the other product
                    $product_cart_id = $cart->generate_cart_id( $settings[$key]['product_id_2'] );
                    
                    // Find other product in cart
                    $in_cart = $cart->find_product_in_cart( $product_cart_id );
                    
                    // When true
                    if ( $in_cart ) {
                        // Hide remove button
                        $link = '';
                        
                        // Break loop
                        break;
                    }
                }
            }
        }
    
        return $link;
    }
    add_filter( 'woocommerce_cart_item_remove_link', 'filter_woocommerce_cart_item_remove_link', 10, 2 );