php - WooCommerce check cart contents on add to cart -
i have 1 product display, offer 2 options it: payment plan (handled through subscription plugin) , single payment. have them displayed on site grouped , both options have "add cart" button. don't want either option in cart @ same time other. either,
a) empty cart before each time add cart clicked.
or
b) check if cart contains product (via productid), , remove payment plan if full pay selected, or vice versa. here i've come option, i'm bit lost , it's not functioning quite right.
global $woocommerce; if ($product_id = 66){ foreach ( $woocommerce->cart->get_cart() $cart_item_key => $cart_item ) { echo $cart_item_key; if($cart_item['product_id'] == '69'){ //remove single product $woocommerce->cart->remove_cart_item($cart_item_key); } } } elseif ($product_id = 69){ foreach ( $woocommerce->cart->get_cart() $cart_item_key => $cart_item ) { echo $cart_item_key; if($cart_item['product_id'] == '66'){ //remove single product $woocommerce->cart->remove_cart_item($cart_item_key); } } }
i'm thinking of adding add_to_cart method before try/catch throw errors. can me figure better solution?
emptying cart easy
add_filter( 'woocommerce_add_to_cart_validation', 'so_31392001_empty_cart', 10, 3 ); function so_31392001_empty_cart( $valid, $product_id, $quantity ) { wc()->cart->empty_cart(); return $valid; }
Comments
Post a Comment