I need to restrict only one varient of variable product in cart woocomerce

Question

I need to restrict only one varient of variable product in cart woocomerce . If same product from different variation is added it should remove old one and add new varient

   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) {

foreach( WC()->cart->get_cart() as $key => $item ){

    // Check if the item to be removed 1 is in cart
    if( $item['product_id'] = '29651' ){
     $key_to_remove1 = $key;
    }

}

    WC()->cart->remove_cart_item($key_to_remove1);


   wc_add_notice( __( 'The product 1 "blab bla" has been removed from cart.', 'theme_domain' ), 'notice' );

}

But this is removing all varient but i need to remove only already added varient and add new varient

0
durai 2 years 2020-01-07T08:41:01-05:00 0 Answers 78 views 0

Leave an answer

Browse
Browse