Jump to content

[SOLVED] limit quantity order for all products


Med6

Recommended Posts

OK, try this:

 

override classes/Cart.php  (see prestashop developers documentation on how) or change directly in classes/Cart.php:  

(Make backup, just in case!)

 

modify the function: (sample code from PS version 1.5.5.0) 

 

public function updateQty($quantity, $id_product, $id_product_attribute = null, $id_customization = false,  $operator = 'up', $id_address_delivery = 0, Shop $shop = null, $auto_add_cart_rule = true)
 
 
...
/* If we have a product combination, the minimal quantity is set with the one of this combination */
if (!empty($id_product_attribute))
  $minimal_quantity = (int)Attribute::getAttributeMinimalQty($id_product_attribute);
else
  $minimal_quantity = (int)$product->minimal_quantity;
 
if (!Validate::isLoadedObject($product))
  die(Tools::displayError());
 
if (isset(self::$_nbProducts[$this->id]))
  unset(self::$_nbProducts[$this->id]);
 
if (isset(self::$_totalWeight[$this->id]))
  unset(self::$_totalWeight[$this->id]);
 
if ((int)$quantity <= 0)
  return $this->deleteProduct($id_product, $id_product_attribute, (int)$id_customization);
elseif (!$product->available_for_order || Configuration::get('PS_CATALOG_MODE'))
  return false;
elseif ($quantity > 4) { // For example, 4 max per same product/product-combination allowed
  return false;
}
else
{
  /* Check if the product is already in the cart */
  $result = $this->containsProduct($id_product, $id_product_attribute, (int)$id_customization,
      (int)$id_address_delivery);
 
  /* Update quantity if product already exist */
  if ($result)
  {
    if ($operator == 'up')
    {
      $sql = 'SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity
          FROM '._DB_PREFIX_.'product p
          '.Product::sqlStock('p', $id_product_attribute, true, $shop).'
          WHERE p.id_product = '.$id_product;
 
      $result2 = Db::getInstance()->getRow($sql);
      $product_qty = (int)$result2['quantity'];
      // Quantity for product pack
      if (Pack::isPack($id_product))
        $product_qty = Pack::getQuantity($id_product, $id_product_attribute);
      $new_qty = (int)$result['quantity'] + (int)$quantity;
      if ($new_qty > 4) { // For example, 4 max per same product/product-combination allowed
        return false;
      }
 
      $qty = '+ '.(int)$quantity;
      ...
 
 
And save the file. Reload the product page and try to add the same product, or the same product combination 5 times. The 5th time it should complain. Try another product or product-combination and this time it should be able again to add to the cart, up to 4 times. the 5th time it should complain again. So we can have of each product or product-combination a max of 4 per order. So you see, you can have unlimited different products per order, but of each product not more than 4 in that order.
 
If you (also) want to limit the maximum amount of products per order (of any product or product-combination), you can have a look in the link Vekia mentioned above. There I limited the total amount of products per order.
 
Hope this helps,
pascal.
  • Like 4
Link to comment
Share on other sites

Hi Vekia,

No he wants not a fixed limit of x products per order, he wants a maximum amount per product, but with possibly more different products per order.

I suggested to open a new topic...

 

pascal

 

 

sorry for my misunderstading i saw a lot of posts related to second topic where you discussed a lot with brilliant code solutions,

i thought that problem was solved and this is only a duplication

 

my bad, now i know that i have to read topic twice before posting  :P

Link to comment
Share on other sites

  • 2 weeks later...

Rom,

well, you could build a module around it that copies the override for you to the correct position, but you still have to change this function, so really without any overrides is not possible as far as I can see now...

There are no hook you could use to change/check the quantity afterwards or cancel the addition afterwards, I believe.

 

pascal.

Link to comment
Share on other sites

ok thank you. 
In fact, my client does not really want to limit the order quantity for these products. He just want to change the operating mode of the cart. 
Instead of incrementing the quantity of products it want it added a line in the cart.
I do not know if I'm clear. 
In the screen below. We see that the same product was added 3 times.
 
120561Sanstitre1.jpg

 

I create a new topic => http://www.prestashop.com/forums/topic/330679-not-update-product-quantity-in-cart-but-create-new-cart-item/

 

Thanks

Link to comment
Share on other sites

  • 2 months later...

hi,

 

I have an issue with prestashop, something similar to this thread.

 

My client the customer just to be able to purchase 1 particular product at a time but the latter can increase its quantity.

For example, let sat i have 3 products.

 

Product A

Product B

Product C

 

At any given type a user can i any product in his cart.(for example Product A), now if i wish to add Product B, i get an error, but if i want to increase the quantity for Product A, i can do that.

 

 

How can i do that with your above code.

Link to comment
Share on other sites

Hi Dursunj,

 

quote:

now if i wish to add Product B, i get an error, but if i want to increase the quantity for Product A, i can do that

 

Do you mean that YOU, as seller should be able to increase afterwards, or the CUSTOMER him/herself can choose more quantity of just one product?

 

pascal

Link to comment
Share on other sites

  • 7 months later...
  • 2 weeks later...

Pietro,

 

For 1.6, just add red code like in my post #4. 1.6 code is almost the same as 1.5 (They added some hook), but if you look at lines just before/after the red lined I added, you should be able to find the correct place in your Cart.php file

 

pascal

Link to comment
Share on other sites

  • 1 year later...

Hi!

 

It worked well on PrestaShop 1.6

 

But  I have another problem:

I want to limit the overall amount of a product, despite the attributes.

 

As example, I limited the quantity for 1.

My product has 2 different attributes (color).

The consumer can buy 1 product of a color and then another 1 product of the other color.

 

But I need to limit this. The consumer could only buy 1 product at all. (but other products per order)

 

Is there a way I can do that?

 

Thanks!

 

Link to comment
Share on other sites

  • 1 year later...
  • 3 months later...

For people who needs to limit the max quantity on only a certain product (which i do), you can try adding the following code.

 

On 4/24/2014 at 1:16 PM, PascalVG said:
if ((int)$quantity <= 0)
  return $this->deleteProduct($id_product, $id_product_attribute, (int)$id_customization);
elseif (!$product->available_for_order || Configuration::get('PS_CATALOG_MODE'))
  return false;
elseif ($quantity > 4 && $id_product =='XXX') { // Where XXX is the product id. For example, 4 max per same product/product-combination allowed
  return false;
}
else
{
  /* Check if the product is already in the cart */
  $result = $this->containsProduct($id_product, $id_product_attribute, (int)$id_customization,
      (int)$id_address_delivery);
 
  /* Update quantity if product already exist */
  if ($result)
  {
    if ($operator == 'up')
    {
      $sql = 'SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity
          FROM '._DB_PREFIX_.'product p
          '.Product::sqlStock('p', $id_product_attribute, true, $shop).'
          WHERE p.id_product = '.$id_product;
 
      $result2 = Db::getInstance()->getRow($sql);
      $product_qty = (int)$result2['quantity'];
      // Quantity for product pack
      if (Pack::isPack($id_product))
        $product_qty = Pack::getQuantity($id_product, $id_product_attribute);
      $new_qty = (int)$result['quantity'] + (int)$quantity;
      if ($new_qty > 4 && $id_product =='XXX') { // Where XXX is the product id. For example, 4 max per same product/product-combination allowed
        return false;
      }

 

Edited by Murphy (see edit history)
Link to comment
Share on other sites

  • 5 months later...

Hi, I'm currently using prestashop 1.7. I would like to limit my customer to order maximum 4 liters in total for any products in one order. (*chemical product which there's limitation of order maximum 4 liters). Any order more than 4 liters is not allowed.

 

May I know how to set a limitation for this case? 

Link to comment
Share on other sites

  • 1 year later...
  • 4 months later...
  • 1 month later...
On 2/9/2018 at 8:02 AM, Murphy said:

For people who needs to limit the max quantity on only a certain product (which i do), you can try adding the following code.

 

 

Just learn prestashop during covid19, and found this helpful article.  if i need to limit max quantity base on category or manufacturer, how can we do it ? i try the code as you provide and change the $id_product with $id_manufacturer. and it's not working. any suggestion ? thanks in advanced

Link to comment
Share on other sites

  • 1 year later...

I posted in this other thread with the code on how to limit the total quantity of particular products that can be added to the cart (eg: can be used to limit the total quantity allowed for all those products in a certain category or by a certain manufacturer, etc)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...