VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: kmeyer on May 21, 2015, 16:41:03 PM

Title: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: kmeyer on May 21, 2015, 16:41:03 PM
If I have an item with 10 units in stock, and I set the quantity-input to 12 and click Add To Cart, the pop-up tells me it added 10xitem to the cart, it is a little hard to notice that I only got 10, when I asked for 12. After I have 10 in the cart, if I try to add 1 more, I get a very nice message like this:
"Max quantity of product XXX reached, new quantity in cart set to 10"
Is there a way to cause this message to display the first time around, when I first exceed the quantity in stock?

Similarly, when I view the cart, if I change the quantity to more than is in stock and click the Update Quantity In Cart icon, I get the Notice at the top of the page "Product quantity successfully updated", but the number stays at the maximum stock level. Here also I'd like the nice warning message to display instead.

My site is test.kurtmeyer.com, running VM 3.0.8 on Joomla 3.3.6.

I have "Action when a Product is Out of Stock" set to "Displays 'Notify Me' instead of 'Add To Cart' button".
Title: Re: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: stanislavus on May 27, 2015, 22:30:06 PM
Hi

did you manage to fix that?
Title: Re: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: kmeyer on June 01, 2015, 14:34:33 PM
No solution yet, anyone out there have any ideas how to fix this? Thanks.
Title: Re: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: siristru on April 12, 2016, 17:21:16 PM
Fix may be easy. Copy this file:

ROOT/components/com_virtuemart/sublayouts/addtocartbar.php

to:

ROOT/templates/your-template/html/com_virtuemart/sublayouts/addtocartbar.php

Then look for this:

            <div class="quantity-box">
<input type="text" class="quantity-input js-recalculate" name="quantity[]"
data-errStr="<?php echo vmText::('COM_VIRTUEMART_WRONG_AMOUNT_ADDED')?>"
value="<?php echo $init?>" init="<?php echo $init?>" step="<?php echo $step?>" <?php echo $maxOrder?> />
</div>


Change it to:

            <div class="quantity-box">
<input type="text" class="quantity-input js-recalculate" name="quantity[]"
data-errStr="<?php echo vmText::('COM_VIRTUEMART_WRONG_AMOUNT_ADDED')?>"
value="<?php echo $init?>" init="<?php echo $init?>" step="<?php echo $step?>" <?php echo 'max="' . ($product->product_in_stock $product->product_ordered) . '"'?> />
</div>


This part is crucial:

<?php echo 'max="' . ($product->product_in_stock $product->product_ordered) . '"'?>

It adds max parameter to quantity field. It calculates available quantity by subtracting ordered product quantity from quantity of products in stock.

Thanks to this no one will be able to add more products to cart than is really available  . No notifications are needed here :)
Title: Re: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: Studio 42 on April 12, 2016, 22:51:34 PM
@siristru
Not exact answer.
If you have really a $maxOrder, this is never used.
so better solution should be :
if($product->product_in_stock - $product->product_ordered < $maxOrder) $maxOrder = $product->product_in_stock - $product->product_ordered;
to add before
<div class="quantity-box">
<input type="text" class="quantity-input js-recalculate" name="quantity[]"
data-errStr="<?php echo vmText::('COM_VIRTUEMART_WRONG_AMOUNT_ADDED')?>"
value="<?php echo $init?>" init="<?php echo $init?>" step="<?php echo $step?>" <?php echo $maxOrder?> />
</div>


Note: this is not right for shop not having stock check and should not be checked/modified, so it's not the right place to put this code

Title: Re: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: sandomatyas on November 24, 2017, 10:10:34 AM
I noticed that the original problem is still there in VM 3.2.4

As kmeyer wrote before let's assume you have a product, and the product_in_stock value is 10. You try to add 15 to cart, and you won't get any warning just a simple COM_VIRTUEMART_CART_PRODUCT_ADDED message where the quantity is 10 (instead 15!) and you got only 10 in the cart. After you try to add 1 or 10 or any you get the proper COM_VIRTUEMART_CART_PRODUCT_OUT_OF_QUANTITY message which you shold get also first time.
I tried to debug it a bit and found method checkForQuantities in class VirtueMartCart and there is this code part
if($productsleft>0){
$quantity = $productsleft;
$product->errorMsg = vmText::sprintf('COM_VIRTUEMART_CART_PRODUCT_OUT_OF_QUANTITY',$product->product_name,$quantity);
vmError($product->errorMsg);
}

Where $productsleft is 10, $quantity is 15 so $quantity will be 10 and according to this code I should get COM_VIRTUEMART_CART_PRODUCT_OUT_OF_QUANTITY message. I debugged it, this is the code part where $quantity is changing so I don't know why VM changes the message later
Title: Re: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: sandomatyas on December 09, 2017, 12:24:10 PM
anybody about this topic? It's very annoying in christmas sale :/
Title: Re: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: Roger Lightening on May 18, 2018, 08:56:55 AM
I am also having the same problem. Any help here would be appreciated.
Title: Re: Cart: Max-quantity warning doesn't show if you add more items than are in stock
Post by: Studio 42 on May 18, 2018, 09:48:34 AM
For the add to cart in product modify the code as explained in sublayouts/addtocartbar.php, this only work when product is not already in cart.
The only (current) solution is to use a product custom plugin and trigger plgVmOnCheckoutCheckStock.
This trigger have the cart content + product + quantity to check as parameters and can display the right message for all cases.