php - How to remove all simple product in the cart Woocommerce -
how remove simple product in cart when click on button:
// add item cart on visit depending on cart total value add_action( 'init', 'add_product_to_cart' ); function add_product_to_cart() { if ( isset($_post['mix']) && !empty($_post['mix']) && isset($_post['mix_name']) && !empty($_post['mix_name']) ) { global $woocommerce; $addcart = $woocommerce->cart->add_to_cart( $product_id = 700, $quantity=1, $variation_id= '', array('attribute_strain' => 'sativa', 'attribute_level' => 'cbd', 'attribute_flavor' => 'flower shop') ); foreach ($woocommerce->cart->cart_contents $cart_item_key => $values) { $_product = $values['data']; //find simple product type remove if($_product->product_type == 'simple') { unset($woocommerce->cart->cart_contents[$cart_item_key]); } } if($addcart) { wp_safe_redirect( wc()->cart->get_checkout_url() ); exit; } else { wc_print_notices(); _e('can\'t add'); } } }
when hit mix button, want add product id = 700 (variation product) cart, , remove other simple products. part adding product ok not remove other simple products. how remove other 'simple' product_type
Comments
Post a Comment