1

Provided solution here Remove a specific cart item when adding to cart a specific product in Woocommerce works perfect for one product id.

Is any way to do this for multiple id's?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
soliddigital
  • 176
  • 3
  • 16

1 Answers1

2

Update 2 : The following code will work for multiple product IDs:

add_action( 'woocommerce_add_to_cart', 'check_product_added_to_cart', 10, 6 );
function check_product_added_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {

    // Set HERE your targeted product ID
    $target_product_ids1 = array(31, 32);
    $target_product_ids2 = array(33, 34);
    // Set HERE the  product ID to remove
    $item_id_to_remove1 = 37;
    $item_id_to_remove2 = 53;

    // Initialising some variables
    $has_item1 = $has_item2 = $is_product_id1 = $is_product_id2 = false;

    foreach( WC()->cart->get_cart() as $key => $item ){
        // Check if the item to be removed 1 is in cart
        if( $item['product_id'] == $item_id_to_remove1 ){
            $has_item1 = true;
            $key_to_remove1 = $key;
        }
        // Check if the item to be removed 2 is in cart
        if( $item['product_id'] == $item_id_to_remove2 ){
            $has_item2 = true;
            $key_to_remove2 = $key;
        }

        // Check if we added to cart any targeted product IDs 1
        if( in_array( $product_id, $target_product_ids1 ) ){
            $is_product_id1 = true;
        }

        // Check if we added to cart any targeted product IDs 2
        if( in_array( $product_id, $target_product_ids2 ) ){
            $is_product_id2 = true;
        }
    }

    if( $has_item1 && $is_product_id1 ){
        WC()->cart->remove_cart_item($key_to_remove1);

        // Optionaly displaying a notice for the removed item:
        wc_add_notice( __( 'The product 1 "blab bla" has been removed from cart.', 'theme_domain' ), 'notice' );
    }

    if( $has_item2 && $is_product_id2 ){
        WC()->cart->remove_cart_item($key_to_remove2);

        // Optionaly displaying a notice for the removed item:
        wc_add_notice( __( 'The product 2 "blab bla" has been removed from cart.', 'theme_domain' ), 'notice' );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi is any way to do it if $target_product_ids = 31 remove $product_to_remove = 32 , if $target_product_ids = 33 remove $product_to_remove = 33 and so on.Thank you – soliddigital Oct 11 '18 at 14:15
  • @DeividasKrupstas I have updated the answer, there was a small mistake. – LoicTheAztec Oct 11 '18 at 15:09
  • Hi on Cart Page i have Cross-sells products and if i add it to cart wc_add_notice not working. But if i add same product from product page wc_add_notice working? – soliddigital Oct 12 '18 at 14:18
  • Hi, how would you remove multiple products (e.g. ID 200 and ID 300) if a certain product (e.g. 100) is added to the cart? – Yaniv Wainer Jan 01 '21 at 01:53