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



11 comments:

  1. Is there a way to set max quantity on individual items or only the shopping cart as a whole?

    ReplyDelete
  2. Is there a way to limit the total cost of the cart?

    ReplyDelete
  3. Somebody knows how to make it works for Prestashop 1.5.2?
    Thanks

    ReplyDelete
  4. same question

    how to make it works for Prestashop 1.5.x?

    thank you

    ReplyDelete
  5. In prestashop 1.5, CartController, line 181 add this (processChangeProductInCart()) :

    $sql = 'SELECT quantity FROM '._DB_PREFIX_.'cart_product as cartp
    WHERE cartp.id_cart = '.$this->context->cart->id.'
    AND cartp.id_product = '.$this->id_product;
    $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
    $product_qty = intval($result[0]['quantity']) + $this->qty;

    if($product_qty > 3 && Tools::getValue('op', 'up') != "down"){
    header('HTTP/1.1 404 page not found');
    $this->errors[] = Tools::displayError('The maximum number of parts per product is 3');
    }
    The code can be better, but it works

    ReplyDelete
    Replies
    1. but it displays an error block. is it possible that it comes back to the page without any indication.? as when the error comes, and we click the link again, it displays that error again and again. so have to clear the history.. :-(

      Delete
    2. You can clean your code with this one:

      $max_qty = 8;

      if($this->context->cart->nbProducts() >= $max_qty && Tools::getValue('op','up') != "down"){
      $this->errors[] = Tools::displayError('The maximum number of products is '.$max_qty, false);
      return;
      }

      Delete
  6. The information written in the article is descriptive and well written.It is also simple to read and understand.Good Read for more knowledge.
    Prestashop Designer

    ReplyDelete
  7. How can the quantity restriction for each product in the cart....

    ReplyDelete
  8. Hi! Could you please help me to adjust it to PS 1.6.1.4?

    ReplyDelete