wcpgsk_maxitems_in_cart

The Woocommerce Poor Guys Swiss Knife wcpgsk maxitems in cart function.

Description

wcpgsk_maxitems_in_cart( $valid, $product_id, $quantity ); 

Parameters (3)

0. $valid
The valid.
1. $product_id
The product id.
2. $quantity
The quantity.

Usage

  1. if ( !function_exists( 'wcpgsk_maxitems_in_cart' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-poor-guys-swiss-knife/wcpgsk-af.php'; 
  3.  
  4. // The valid. 
  5. $valid = null; 
  6.  
  7. // The product id. 
  8. $product_id = null; 
  9.  
  10. // The quantity. 
  11. $quantity = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wcpgsk_maxitems_in_cart($valid, $product_id, $quantity); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wcpgsk-af.php  
  1. function wcpgsk_maxitems_in_cart( $valid, $product_id, $quantity ) { 
  2. global $woocommerce; 
  3. $options = get_option( 'wcpgsk_settings' ); 
  4.  
  5. $cartItems = sizeof( $woocommerce->cart->cart_contents ); 
  6. $allowed = isset($options['cart']['maxitemscart']) && $options['cart']['maxitemscart'] != 0 ? $options['cart']['maxitemscart'] : 0; 
  7.  
  8. //check cart items count and diminish if more than one variation for a product exists 
  9. if ( $allowed > 0 && isset($options['cart']['variationscountasproduct']) && $options['cart']['variationscountasproduct'] == 0) {  
  10. $varproducts = array(); 
  11. foreach($woocommerce->cart->cart_contents as $i => $values) { 
  12. $key = $values['product_id']; 
  13. //@TODO: Check layout of the question and answer 
  14. if (isset($values[$key]) && isset($values['variation_id']) && $values[$key] != $values['variation_id']) { 
  15. if (isset($varproducts[$key])) $varproducts[$key] = 1; 
  16. else $varproducts[$key] = 0; 
  17. if (!empty($varproducts)) $cartItems = $cartItems - array_sum($varproducts); 
  18.  
  19. if ( $allowed > 0 && $cartItems >= $allowed ) { 
  20. // Setserrormessage. 
  21. wcpgsk_add_message( sprintf( __( 'You have reached the maximum amount of %s items allowed for your cart!', WCPGSK_DOMAIN ), $allowed ) ); 
  22. $valid = false; 
  23. $cart_url = $woocommerce->cart->get_cart_url(); 
  24. wcpgsk_add_message( __('Remove products from the cart', WCPGSK_DOMAIN) . ': <a href="' . $cart_url . '">' . __('Cart', WCPGSK_DOMAIN) . '</a>'); 
  25. }  
  26. return $valid;