/** * 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(); } } }
function wcpgsk_add_cart_item($cart_item_data, $cart_item_key) { global $woocommerce; $options = get_option('wcpgsk_settings'); $product_id = $cart_item_data['product_id']; $variation_id = $cart_item_data['variation_id']; $product = get_product($product_id); $quantity = $cart_item_data['quantity']; $maxqty = isset($options['cart']['maxqty_' . $product->product_type]) ? $options['cart']['maxqty_' . $product->product_type] : 0; $minqty = isset($options['cart']['minqty_' . $product->product_type]) ? $options['cart']['minqty_' . $product->product_type] : 0; $stepqty = isset($options['cart']['stepqty_' . $product->product_type]) ? $options['cart']['stepqty_' . $product->product_type] : 0; if (isset($options['cart']['minmaxstepproduct']) && $options['cart']['minmaxstepproduct'] == 1) { $maxval = get_post_meta($product_id, '_wcpgsk_maxqty', true); if (isset($maxval) && $maxval > 0) { $maxqty = $maxval; } $minval = get_post_meta($product_id, '_wcpgsk_minqty', true); if (isset($minval) && $minval > 0) { $minqty = $minval; } $stepval = get_post_meta($product_id, '_wcpgsk_stepqty', true); if (isset($stepval) && $stepval > 0) { $stepqty = $stepval; } } if ($minqty > 0 && $quantity < $minqty) { $cart_item_data['quantity'] = $minqty; wcpgsk_clear_messages(); 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)); wcpgsk_set_messages(); } elseif ($maxqty > 0 && $quantity > $maxqty) { $cart_item_data['quantity'] = $maxqty; wcpgsk_clear_messages(); wcpgsk_add_message(sprintf(__('You cannot buy more than the allowed maximum quantity. We have set the allowed maximum of %s as quantity for you.', WCPGSK_DOMAIN), $maxqty)); wcpgsk_set_messages(); } elseif ($stepqty > 0 && $quantity % $stepqty > 0) { $remainder = $quantity % $stepqty; $newqty = $quantity - $remainder; if ($newqty > 0) { $cart_item_data['quantity'] = $newqty; } else { $newqty = $stepqty; $cart_item_data['quantity'] = $newqty; } wcpgsk_clear_messages(); 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)); wcpgsk_set_messages(); } return $cart_item_data; }