MOD: smarter min/max quantity enforcement

CS-Cart allows you define a product’s Minimum Quantity and/or Maximum Quantity. Unfortunately, the cart logic has a rather narrow definition of these quantity rules.



For instance, a customer buying a shirt (min qty: 6) can only satisfy the minimum with 6 of a particular size. 3 of size Medium + 3 of size Large does not meet the requirement.



Likewise, if the same shirt has a Maximum Quantity of 12, the cart will allow the customer to buy 12 Smalls, 12 Mediums, etc. because it sees each variation as a unique product.



If you happen to be using the Product Configurator addon, you’ll notice that the Minimum/Maximum Quantities only apply to each Configured Product’s children - not across multiple Configured Products.



To correct this, we need to edit /core/fn.cart.php.



Find

```php if ($amount < $min_qty) {

fn_set_notification(‘W’, fn_get_lang_var(‘notice’), str_replace(array(‘[product]’ , ‘[quantity]’), array($product[‘product’] , $min_qty), fn_get_lang_var(‘text_cart_min_qty’)));

$amount = $min_qty;

} elseif (!empty($product[‘max_qty’]) && $amount > $product[‘max_qty’]) {

$amount = $product[‘max_qty’];

fn_set_notification(‘W’, fn_get_lang_var(‘notice’), str_replace(array(‘[product]’ , ‘[quantity]’), array($product[‘product’], $product[‘max_qty’]), fn_get_lang_var(‘text_cart_max_qty’))); ```



And replace it with

```php // START // Min Max Quantities with Options Mod

$max_qty = $product[‘max_qty’];

$new_amount = $amount;



foreach ($_SESSION[‘cart’][‘products’] as $k => $v) { //poll the cart contents for existing items

if (($v[‘product_id’] == $product_id) && ($k != $cart_id)) {

$new_amount += $v[‘amount’];

}

}



if (!empty($max_qty) && $new_amount > $max_qty) {



if ($new_amount > $amount) { //this item already exists in the cart

if (($new_amount - $amount) >= $max_qty) { //the existing item already hits the max qty

$amount = 0; //no more room for the new item

$min_qty = 0; //prevent the min_qty alert

fn_set_notification(‘W’, fn_get_lang_var(‘notice’), str_replace(array(‘[product]’ , ‘[quantity]’), array($product[‘product’], $max_qty), fn_get_lang_var(‘text_cart_max_qty’)));

} else { //limit new item’s quantity

if ($new_amount > $max_qty) {

$amount = $max_qty - ($new_amount - $amount);

fn_set_notification(‘W’, fn_get_lang_var(‘notice’), str_replace(array(‘[product]’ , ‘[quantity]’), array($product[‘product’], $max_qty), fn_get_lang_var(‘text_cart_max_qty’))); }

}



} else { //this is the only occurance of this item in the cart

if ($amount > $max_qty) {

$amount = $max_qty; //limit new item’s quantity

fn_set_notification(‘W’, fn_get_lang_var(‘notice’), str_replace(array(‘[product]’ , ‘[quantity]’), array($product[‘product’], $max_qty), fn_get_lang_var(‘text_cart_max_qty’)));

}

}

}





if ($new_amount >= $min_qty) { //the existing item already hits the min qty

$min_qty = $amount;

}



if ($amount < $min_qty) {



fn_set_notification(‘W’, fn_get_lang_var(‘notice’), str_replace(array(‘[product]’ , ‘[quantity]’), array($product[‘product’] , $min_qty), fn_get_lang_var(‘text_cart_min_qty’)));



$_SESSION[‘notifications’] = fn_array_unique($_SESSION[‘notifications’]); //suppress duplicate error messages

if ($_REQUEST[‘dispatch’] == ‘checkout.checkout’) { //don’t allow checkout until min_qty satisfied

header(‘Location: index.php?dispatch=checkout.cart’);

}





// END // Min Max Quantities with Options Mod ```



Highlights of the new functionality:

  • Minimum Quantity is satisfied with different product options or Configured Products
  • customer is not forced to satisfy the Minimum Quantity until Proceed to Checkout
  • Maximum Quantity limits are hit with different product options or Configured Products



    Note: you may want to adjust the text_cart_max_qty and text_cart_min_qty language to better reflect the above functionality. This was done in CS-Cart 2.1.1 but I’ve also applied it to a 2.0.14 install.



    cheers,

    Glen

Thanks for this Glen,



just what I need.



John

I don’t think this is a problem to be “fixed”. This is a feature that was long requested to ensure that individual product (small shirts) would be inventoried and tracked separately than other related products (medium shirts).



This way, a 12 shirt maximum applies to each configured product separately.

[quote]This is a feature that was long requested to ensure that individual product (small shirts) would be inventoried and tracked separately than other related products (medium shirts).[/quote]



I guess I don’t follow you. This modification does not change the existing inventory controls. If item “shirt” has inventory defined for each “size”, the cart will continue to ensure that those individual inventory amounts are not exhausted by the user.



Personally, I don’t have any use for the standard Maximum Quantity field. I’ve only seen it on sites like shop.lego.com where they’re trying to limit the grey market.



In my case, I use Maximum Quantity so that I can apply a single Required Product “Embroidery Setup Charge” (Maximum Quantity = 1) to each order. I disabled the Maximum Quantity alert so that the customer doesn’t see that every time they add an embroidered item to their cart.



My supplier’s drop-ship apparel products do require a Minimum Quantity, but I can mix and match size/color - making this mod necessary.



cheers,

Glen

Sounds like your doin the same thing as me glen, emb and printed gear.

I have a table option qty addition MOD that CS did for me if its any good to you, what it allows is all the sizes to be entered on the 1 order page instead of adding 2 large, then 3 medium etc etc it lists all sizes in a table for one order hit, and its up to you how you jazz up the table.



John

[quote]I have a table option qty addition MOD that CS did for me if its any good to you, what it allows is all the sizes to be entered on the 1 order page instead of adding 2 large, then 3 medium etc etc it lists all sizes in a table for one order hit, and its up to you how you jazz up the table.[/quote]



That was next on my to-do list, but I know CSC doesn’t like having their work shared here. I’ll shoot you a PM.



Glen