wcpgsk_minitems_in_cart

Validate product quantity on cart update.

Description

wcpgsk_minitems_in_cart( $valid, $cart_item_key, $values, $quantity ); 

Parameters (4)

0. $valid
The valid.
1. $cart_item_key
The cart item key.
2. $values
The values.
3. $quantity
The quantity.

Usage

  1. if ( !function_exists( 'wcpgsk_minitems_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 cart item key. 
  8. $cart_item_key = null; 
  9.  
  10. // The values. 
  11. $values = null; 
  12.  
  13. // The quantity. 
  14. $quantity = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wcpgsk_minitems_in_cart($valid, $cart_item_key, $values, $quantity); 
  18.  

Defined (1)

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

/wcpgsk-af.php  
  1. function wcpgsk_minitems_in_cart( $valid, $cart_item_key, $values, $quantity ) { 
  2. global $woocommerce; 
  3. $options = get_option( 'wcpgsk_settings' ); 
  4.  
  5. //$cartItems = $woocommerce->cart->get_cart_contents_count(); //counts quantities as well and not only items 
  6. $cartItems = sizeof( $woocommerce->cart->cart_contents ); 
  7. $allowed = isset($options['cart']['minitemscart']) && $options['cart']['minitemscart'] != 0 ? $options['cart']['minitemscart'] : 0; 
  8.  
  9. //check cart items count and diminish if more than one variation for a product exists 
  10. if ($allowed > 1 && isset($options['cart']['variationscountasproduct']) && $options['cart']['variationscountasproduct'] == 0) {  
  11. $varproducts = array(); 
  12. foreach($woocommerce->cart->cart_contents as $i => $values) { 
  13. $key = $values['product_id']; 
  14. //@TODO: Check layout of the question and answer  
  15. if (isset($values[$key]) && isset($values['variation_id']) && $values[$key] != $values['variation_id']) { 
  16. if (isset($varproducts[$key])) $varproducts[$key] = 1; 
  17. else $varproducts[$key] = 0; 
  18. if (!empty($varproducts)) $cartItems = $cartItems - array_sum($varproducts); 
  19.  
  20. if ($allowed > 1 && $allowed > $cartItems ) { 
  21. // Setserrormessage. 
  22. wcpgsk_add_message( sprintf( __( 'You still have not reached the minimum amount of %s items required for your cart!', WCPGSK_DOMAIN ), $allowed ) ); 
  23. $valid = false; 
  24.  
  25. $shop_page_id = woocommerce_get_page_id( 'shop' ); 
  26. //$shop_page_url = get_permalink(icl_object_id($shop_page_id, 'page', false)); 
  27. $shop_page_url = get_permalink($shop_page_id); 
  28. wcpgsk_add_message( __('Select more products from the shop', WCPGSK_DOMAIN) . ': <a href="' . $shop_page_url . '">' . __('Shop', WCPGSK_DOMAIN) . '</a>'); 
  29.  
  30. }  
  31. return $valid;