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

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -