/**
  * Check for minimum and maximum items in cart and if they fulfill the settings.
  * and raise error and message if rules are not fulfilled, otherwise clear messages
  *
  * @access public
  * @param mixed $checkout
  * @since 1.1.0
  * @return void
  */
 public function wcpgsk_checkout_init($checkout)
 {
     global $woocommerce;
     $options = get_option('wcpgsk_settings');
     $cartItems = sizeof($woocommerce->cart->cart_contents);
     $allowed = $options['cart']['maxitemscart'];
     //check cart items count and diminish if more than one variation for a product exists
     if ($allowed > 0 && isset($options['cart']['variationscountasproduct']) && $options['cart']['variationscountasproduct'] == 0) {
         $varproducts = array();
         foreach ($woocommerce->cart->cart_contents as $i => $values) {
             $key = $values['product_id'];
             if (isset($values[$key]) && isset($values[$variation_id]) && $values[$key] != $values['variation_id']) {
                 if (isset($varproducts[$key])) {
                     $varproducts[$key] = 1;
                 } else {
                     $varproducts[$key] = 0;
                 }
             }
         }
         if (!empty($varproducts)) {
             $cartItems = $cartItems - array_sum($varproducts);
         }
     }
     if ($allowed > 0 && $cartItems > $allowed) {
         wcpgsk_clear_messages();
         // Sets error message.
         wcpgsk_add_error(sprintf(__('You have reached the maximum amount of %s items allowed for your cart!', WCPGSK_DOMAIN), $allowed));
         wcpgsk_set_messages();
         $cart_url = $woocommerce->cart->get_cart_url();
         wcpgsk_add_message(__('Remove products from the cart', WCPGSK_DOMAIN) . ': <a href="' . $cart_url . '">' . __('Cart', WCPGSK_DOMAIN) . '</a>');
         wcpgsk_set_messages();
         //wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) );
         //exit;
     } else {
         $allowed = $options['cart']['minitemscart'];
         //check cart items count and diminish if more than one variation for a product exists
         if ($allowed > 1 && isset($options['cart']['variationscountasproduct']) && $options['cart']['variationscountasproduct'] == 0) {
             $varproducts = array();
             foreach ($woocommerce->cart->cart_contents as $i => $values) {
                 $key = $values['product_id'];
                 if (isset($values[$key]) && isset($values['variation_id']) && $values[$key] != $values['variation_id']) {
                     if (isset($varproducts[$key])) {
                         $varproducts[$key] = 1;
                     } else {
                         $varproducts[$key] = 0;
                     }
                 }
             }
             if (!empty($varproducts)) {
                 $cartItems = $cartItems - array_sum($varproducts);
             }
         }
         if ($allowed > 1 && $allowed > $cartItems) {
             // Sets error message.
             wcpgsk_clear_messages();
             wcpgsk_add_error(sprintf(__('You still have not reached the minimum amount of %s items required for your cart!', WCPGSK_DOMAIN), $allowed));
             wcpgsk_set_messages();
             $valid = false;
             $shop_page_id = woocommerce_get_page_id('shop');
             //$shop_page_url = get_permalink(icl_object_id($shop_page_id, 'page', false));
             $shop_page_url = get_permalink($shop_page_id);
             wcpgsk_add_message(__('Select more products from the shop', WCPGSK_DOMAIN) . ': <a href="' . $shop_page_url . '">' . __('Shop', WCPGSK_DOMAIN) . '</a>');
             wcpgsk_set_messages();
             //wp_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
             //exit;
         } else {
             wcpgsk_clear_messages();
         }
     }
 }
Esempio n. 2
0
 /**
 * Validate product quantity on cart update.
 */
 function wcpgsk_minitems_in_cart($valid, $cart_item_key, $values, $quantity)
 {
     global $woocommerce;
     $options = get_option('wcpgsk_settings');
     //$cartItems = $woocommerce->cart->get_cart_contents_count(); //counts quantities as well and not only items
     $cartItems = sizeof($woocommerce->cart->cart_contents);
     $allowed = isset($options['cart']['minitemscart']) && $options['cart']['minitemscart'] != 0 ? $options['cart']['minitemscart'] : 0;
     //check cart items count and diminish if more than one variation for a product exists
     if ($allowed > 1 && isset($options['cart']['variationscountasproduct']) && $options['cart']['variationscountasproduct'] == 0) {
         $varproducts = array();
         foreach ($woocommerce->cart->cart_contents as $i => $values) {
             $key = $values['product_id'];
             //@TODO: Check layout of the question and answer
             if (isset($values[$key]) && isset($values['variation_id']) && $values[$key] != $values['variation_id']) {
                 if (isset($varproducts[$key])) {
                     $varproducts[$key] = 1;
                 } else {
                     $varproducts[$key] = 0;
                 }
             }
         }
         if (!empty($varproducts)) {
             $cartItems = $cartItems - array_sum($varproducts);
         }
     }
     if ($allowed > 1 && $allowed > $cartItems) {
         // Sets error message.
         wcpgsk_add_message(sprintf(__('You still have not reached the minimum amount of %s items required for your cart!', WCPGSK_DOMAIN), $allowed));
         wcpgsk_set_messages();
         $valid = false;
         $shop_page_id = woocommerce_get_page_id('shop');
         //$shop_page_url = get_permalink(icl_object_id($shop_page_id, 'page', false));
         $shop_page_url = get_permalink($shop_page_id);
         wcpgsk_add_message(__('Select more products from the shop', WCPGSK_DOMAIN) . ': <a href="' . $shop_page_url . '">' . __('Shop', WCPGSK_DOMAIN) . '</a>');
         wcpgsk_set_messages();
     }
     return $valid;
 }