php - Woo commerce only 1 item purchase -
i have implemented following code in woo commerce site in functions.php of theme, however, when adding it, site goes blank. ideas why? in advance help.
function check_if_cart_has_product( $valid, $product_id, $quantity ) { if(!empty(wc()->cart->get_cart()) && $valid){ foreach (wc()->cart->get_cart() $cart_item_key => $values) { $_product = $values['data']; if( $product_id == $_product->id ) { unset(wc()->cart->cart_contents[$cart_item_key]); } } } return $valid; } add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );
to access cart use:
gloabl $woocommerce; $woocommerce->cart->get_cart(); here available methods class:
http://docs.woothemes.com/wc-apidocs/class-wc_cart.html
also read:
debug in wp
https://codex.wordpress.org/wp_debug
Comments
Post a Comment