wcpgsk_check_cart_items

The Woocommerce Poor Guys Swiss Knife wcpgsk check cart items function.

Description

wcpgsk_check_cart_items(); 

Usage

  1. if ( !function_exists( 'wcpgsk_check_cart_items' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-poor-guys-swiss-knife/wcpgsk-af.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wcpgsk_check_cart_items(); 
  7.  

Defined (1)

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

/wcpgsk-af.php  
  1. function wcpgsk_check_cart_items() { 
  2. global $woocommerce; 
  3. //wcpgsk_clear_messages(); 
  4. $options = get_option( 'wcpgsk_settings' ); 
  5. $qtycnt = 0; 
  6. $prodcnt = 0; 
  7. $do_cart_redirect = false; 
  8. foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) : 
  9. $product_id = $values['product_id'];  
  10. $quantity = $values['quantity']; 
  11. $qtycnt += $quantity; 
  12. $prodcnt++; 
  13. $product = get_product( $product_id ); 
  14.  
  15. $minqty = isset($options['cart']['minqty_' . $product->product_type]) && $options['cart']['minqty_' . $product->product_type] ? $options['cart']['minqty_' . $product->product_type] : 0; 
  16. $maxqty = isset($options['cart']['maxqty_' . $product->product_type]) && $options['cart']['maxqty_' . $product->product_type] ? $options['cart']['maxqty_' . $product->product_type] : 0; 
  17. $stepqty = isset($options['cart']['stepqty_' . $product->product_type]) && $options['cart']['stepqty_' . $product->product_type] ? $options['cart']['stepqty_' . $product->product_type] : 0; 
  18. if ( isset($options['cart']['minmaxstepproduct']) && $options['cart']['minmaxstepproduct'] == 1 ) :  
  19. $maxval = get_post_meta($product_id, '_wcpgsk_maxqty', true); 
  20. if ( isset($maxval) && $maxval > 0 ) : 
  21. $maxqty = $maxval; 
  22. endif; 
  23. $minval = get_post_meta($product_id, '_wcpgsk_minqty', true); 
  24. if ( isset($minval) && $minval > 0 ) : 
  25. $minqty = $minval; 
  26. endif; 
  27. $stepval = get_post_meta($product_id, '_wcpgsk_stepqty', true); 
  28. if ( isset($stepval) && $stepval > 0 ) : 
  29. $stepqty = $stepval; 
  30. endif; 
  31. endif; 
  32. if ( $minqty > 0 && $quantity < $minqty ) : 
  33. $woocommerce->cart->set_quantity( $cart_item_key, $minqty ); 
  34. if ( is_checkout() ) : 
  35. wcpgsk_add_error( sprintf( __( 'You have to buy a minimum quantity. We have set the required minimum of %s as quantity for you.', WCPGSK_DOMAIN ), $minqty ) ); 
  36. $do_cart_redirect = true; 
  37. else : 
  38. wcpgsk_add_message( sprintf( __( 'You have to buy a minimum quantity. We have set the required minimum of %s as quantity for you.', WCPGSK_DOMAIN ), $minqty ) ); 
  39. endif; 
  40. elseif ( $maxqty > 0 && $quantity > $maxqty ) : 
  41. $woocommerce->cart->set_quantity( $cart_item_key, $maxqty ); 
  42. if ( is_checkout() ) : 
  43. wcpgsk_add_error( sprintf( __( 'You cannot buy more than the allowed quantity for this product. We have set the maximum of %s as quantity for you.', WCPGSK_DOMAIN ), $maxqty ) ); 
  44. $do_cart_redirect = true; 
  45. else : 
  46. wcpgsk_add_message( sprintf( __( 'You cannot buy more than the allowed quantity for this product. We have set the maximum of %s as quantity for you.', WCPGSK_DOMAIN ), $maxqty ) ); 
  47. endif; 
  48. elseif ( $stepqty > 0 && ( $quantity % $stepqty ) > 0 ) : 
  49. $remainder = $quantity % $stepqty; 
  50. $newqty = $quantity - $remainder; 
  51. if ( $newqty > 0 ) : 
  52. $woocommerce->cart->set_quantity( $cart_item_key, $newqty ); 
  53. else : 
  54. $newqty = $stepqty; 
  55. $woocommerce->cart->set_quantity( $cart_item_key, $newqty );  
  56. endif; 
  57.  
  58. if ( is_checkout() ) : 
  59. wcpgsk_add_error( sprintf( __( 'You have to buy this product in multiples of %s. We have set the product quantity to the closest lower multiple available.', WCPGSK_DOMAIN ), $stepqty ) ); 
  60. $do_cart_redirect = true; 
  61. else : 
  62. wcpgsk_add_message( sprintf( __( 'You have to buy this product in multiples of %s. We have set the product quantity to the closest lower multiple available.', WCPGSK_DOMAIN ), $stepqty ) ); 
  63. endif; 
  64. endif; 
  65. endforeach; 
  66. $maxqtycart = isset( $options['cart']['maxqtycart'] ) ? $options['cart']['maxqtycart'] : 0; 
  67. $minqtycart = isset( $options['cart']['minqtycart'] ) ? $options['cart']['minqtycart'] : 0; 
  68. if ( $maxqtycart && is_numeric($maxqtycart) && $qtycnt > $maxqtycart ) :  
  69. if ( is_checkout() ) : 
  70. wcpgsk_add_error( sprintf( __( 'The overall sum for all product quantities is restricted to %s in this shop. Your overall quantity sum: %s. Please buy less quantity at least for some products.', WCPGSK_DOMAIN ), $maxqtycart, $qtycnt ) ); 
  71. $do_cart_redirect = true; 
  72. else : 
  73. wcpgsk_add_message( sprintf( __( 'The overall sum for all product quantities is restricted to %s in this shop. Your overall quantity sum: %s. Please buy less quantity at least for some products.', WCPGSK_DOMAIN ), $maxqtycart, $qtycnt ) ); 
  74. endif; 
  75. endif;  
  76. if ( $minqtycart && is_numeric($minqtycart) && $qtycnt < $minqtycart ) :  
  77. if ( is_checkout() ) : 
  78. wcpgsk_add_error( sprintf( __( 'The required minimum sum for all product quantities is set to %s in this shop. Your overall quantity sum: %s. You have to add products to your cart or buy existing products in a higher quantity.', WCPGSK_DOMAIN ), $minqtycart, $qtycnt ) ); 
  79. $do_cart_redirect = true; 
  80. else : 
  81. wcpgsk_add_message( sprintf( __( 'The required minimum sum for all product quantities is set to %s in this shop. Your overall quantity sum: %s. You have to add products to your cart or buy existing products in a higher quantity.', WCPGSK_DOMAIN ), $minqtycart, $qtycnt ) ); 
  82. endif; 
  83. endif;  
  84. if ( $do_cart_redirect ) : 
  85. wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) );  
  86. endif;