Jump to content
Med6

[SOLVED] limit quantity order for all products

Recommended Posts

Hello

I want to set the maximum quantity  order of a product 

if I have a product P.. only 4 items of this Product  is allowed per customer

 

Share this post


Link to post
Share on other sites

i suggest to use module like mine maximum product quantity

it allows to define what is the maximum quantity of product in cart, or what is the quantity limit for all orders

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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 3

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Hi Med6,

Glad it works :-)

 

I'll mark the topic as solved.

 

Happy selling,

pascal

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

I need to allow to buy only one product/user, including order history. The best solution would be to inform when adding to cart that he already has bought this product.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Hi Pietro,

thanks for the confirmation. Good to hear!

 

 

Happy selling,

pascal

  • Like 1

Share this post


Link to post
Share on other sites

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!

 

Share this post


Link to post
Share on other sites

Works nice on 1.6.1.7..... but DON'T FORGET to add an "}" at the start of the first red line
(2 hours wasted due to my lack of concentration :rolleyes:)

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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? 

Share this post


Link to post
Share on other sites

Any one kindly update... How to limit one customer maximum 5 qty of sky products at a time per day....

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More