Thursday, September 29, 2011

Prestashop: define maximum number of items in shopping cart

I searched over the internet for a day to find out how to limit a user to buy a certain quantity of products in prestashop. There is an option to set minimum amount for the order to validate, but there is no way you can define maximum number of items for your prestashop shopping cart.

So i did it by modifying some code, here how to do this:

Edit file located at controllers/OrderController.php

Go to line 45 and replace

global $isVirtualCart, $orderTotal;

with

global $isVirtualCart, $orderTotal, $cart, $cookie;

Now go to line 67 and paste below code on or after line 67


$max_items = 2;
$cart_rs = mysql_query("select sum(quantity) as item_count from "._DB_PREFIX_."cart_product where id_cart='".$cart->id."'");
$row = mysql_fetch_assoc($cart_rs);
$cart_count = $row["item_count"];
if ($cart_count > $max_items)
{
$this->step = 0;
$this->errors[] = Tools::displayError('Maximum '.$max_items.' items allowed per order');
}

You can change the value of variable $max_items to set your desired maximum number of items allowed in shopping cart.

This code works fine with prestashop 1.4 or higher.

Any suggestions or problems, please do let me know.

Good day