We are using xquantity to do volume buying price adjustments on a website I am building. Unfortunately, we are having an issue where the discounted price is not computed on the cart view until the quantity is changed and update cart is pressed.
For example, if I add 400 of a product to my cart (via the quantity field on the product page/add to cart form) that costs $1.50 and it is supposed to give a $0.50 discount per unit, and then press the cart button and go to the cart view, not only will the unit price be displayed at $1.50 but the total will be displayed at $600, which is not correct. At this point, if I adjust quantity up to 401 and press "update cart", the page refreshed and the price is correctly displayed at $1.00 with a total of $401, at this point I can adjust the quantity back down to 400, press update cart, and the price will be displayed correctly at $1.00 with a total of $400. I do not have the checkout flow set up yet, so I'm not sure if the price simply won't be computed again if the customer presses checkout, or if the price will update correctly on the checkout page, but even if it does work correctly, I really need the cart to display the right price.
Unfortunately, unless the quantity is changed, pressing update cart by itself will not fix the price (I considered writing some hacky javascript to automatically press the update cart button for you when you go to the cart, but I would need it to change the quantity, press update cart, change the quantity back, and then press update cart again triggering a refresh both times), and then letting the customer to do their thing, but that's not really a workable solution, it'll be hacky at best and downright insufferable at worst.
I have also considered just taking the quantity field off of the add to cart form on the product page, but that's where the quantity pricing notification is displayed (instead of the cart, as every product has it's own pricing scheme and if you have a dozen different products in your cart you will be inundated with quantity pricing notifications) and we cater to wholesalers, so order volumes are pretty large.
Any ideas on what might be going on? Is this a bug in xquantity or am I missing something obvious?
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | 3081003-21.patch | 2.11 KB | drugan |
| #10 | qty-price-adjustment-3081003-10.patch | 1.87 KB | sekoz |
| #5 | cart_updated.png | 611.55 KB | Gode.Agarunov |
| #5 | cart.png | 448.49 KB | Gode.Agarunov |
| #5 | product_1.png | 361.85 KB | Gode.Agarunov |
Comments
Comment #2
Gode.Agarunov commentedComment #3
Gode.Agarunov commentedSo I've been working on this and I was able to get a little farther by including this code:
in src/views/field/xquantityeditquantity.php inside the viewsform() function, using code I basically jacked from the viewsformsubmit()
Doing this, I can now just refresh the cart page after adding 400 of an item from the product page and clicking cart, and the correct price is displayed without having to change the quantity and hit "update cart", but if you go to checkout the item total is correct but the cart total is still the wrong number. Obviously this is because I'm not actually recomputing the cart price in the above hacky garbage I wrote.
However, it seems like this makes it quite clear that the module isn't running the quantity discount price check stuff, either because I misconfigured something somewhere or it's a use case that wasn't considered when it was created. I will continue trying to figure out what is going on here, but I would appreciate any help I can get.
Comment #4
drugan commentedCan't reproduce your issue.
First, ensure that quantity price adjustments are done on the ADD TO CART form display mode on the respective order item type.
Second, at least for debugging purposes enable both form and cart page adjustment notification messages as it shown in the screenshot. Also, ensure that starting quantity for your adjustment 400, not 401.
Then, after adding variation to a cart play with quantity increase / decrease on the your-site.come/cart page.
Please, keep in mind that quantity price adjusments have no AJAX support so the actually adjusted price could be seen only after refreshing a page either by pressing Add to cart or Update cart button on the /cart page.
Comment #5
Gode.Agarunov commented@drugan
I followed your instructions and verified everything, everything seems to be configured correctly. It kind of seems related to the ajax support, but I don't use AJAX for the cart, when you hit "add to cart" on a product the page itself refreshes on the product page, and then you can click on the cart icon to go to your cart and see the item(s) you added.
Here are some screenshots of my configuration and what is happening, I threw ksm()s in a couple of places because I figured it might help.
Settings:

KSM on product page:

Product page with notification:

After changing quantity to 500 on the product page, clicking "add to cart", waiting for page refresh, and then hitting the cart menu item:

Changed the quantity to 499 (the step is 250-700 so it shouldn't matter), clicked "update cart", and then the price and total updated:

It's very strange, I think I did something wrong somewhere.
Comment #6
Gode.Agarunov commentedSo this is how I ended up fixing it, it's hacky, but it works:
Inside of commerce_xquantity/src/plugin/views/field/xquantityeditquantity.php
and then I just do this inside my js:
Comment #7
drugan commentedOh, you've done such a great work!
A bit later I'll test your code but for now I'd recommend to NOT use the qty end setting if the the next setting for the quantity adjustment already exists. So, on your screenshot the 99, 149, 199 and 249 are redundant because they anyway overridden by the next qty start 100, 150, 200 and 250 qty start settings. Just the 700 qty end is need in case if you don't want to apply any price adjustments after this quantity. But I guess that starting from the very last qty start 250 you'd want to make this price adjustment till to infinity product variation quantity? If that's true then the 700 qty end is also does NOT need.
If you visit the http://yoursite.com/admin/help/commerce_xquantity#qty-end page you'll find there an explanation to be cautious with the qty end setting as that may disable some quantity ranges from being adjusted.
Comment #8
Gode.Agarunov commented@drugan
Thanks! I took most of the code from viewsformsubmit() lol, as I noticed that when I updated the quantity on cart and submitted the views form everything would update correctly, so I figured there is something going on in the form submit that isn't being done when the form is being built.
I set end quantities initially because I was worried that if there was no end quantity, it would apply both adjustments to the price, which is kind of silly looking back at it now, but for some reason I figured that would be the default behavior with no end quantity set. I will remove them all.
I did a few more things, got rid of some redundant lines and pulled what I could out of the recursion so that the cart doesn't need to be loaded and saved for each row/product on the cart view, I will include that below.
I do have an unrelated question though, if I have a product, lets say a phone case, and it comes in two colors, and I have one quantity discount set up for a minimum quantity of 10 items. Will the adjustment still trigger if I add 6 of each variation (for a total of 12) to my cart? I am assuming it will not because they are separate order items, but ideally I would actually like it to combine variation quantities before computing whether or not the discount applies. I understand how complex that would be to actually implement though, and then the majority of the community may not even want it to behave in such a way, but it would be nice as a feature you could enable with a checkbox or something in a future version.
Anyways, here is the final code, thank you for your help. It still requires JS to force the refresh as I do not know how to force the views form to rebuild programmatically, if that is even something that can be done. Anywhere I added code I put /* Custom - explanation */, there are 3 snippets I added.
Comment #9
drugan commentedStrictly speaking any product variation is different even if it looks like for us humans being ,,basically,, the same.
What I'd recommend is to play with the YOUR_MODULE_xquantity_add_to_cart_qty_prices_alter() hook so you can apply your own business logic while adjusting quantity prices. Just look into the currently viewing product variation or order item if it is a /cart page, then load the shopping cart for the current user, then if there is already added let say 6 green color variations then try to alter the qty start for this adjustment to start from 4 instead of 10 if the red color variation of the same product will added. See more:
http://yoursite.com/admin/help/commerce_xquantity#notify
Comment #10
sekoz commentedI am having the same issue on my site. Anything added through the Add to cart won't register as a volume discount, I need to change the quantity from the /cart page and click update cart.
And my qty end values are all empty.
Hopefully, @godeagarunov saved the day, I turned his code into a temporary patch.
Comment #12
drugan commentedOh, sorry guys, that was my stupid error because I was developing / testing against decimal quantity #step not taking into account integer #step. So, in this case in the XquantityAddToCartForm.php on this line the adjustment have never being applied:
Obviously, an integer #step has $scale == 0, hence a not applied adjusment bug))
Please, test it now and if it also works for you then I'll create a quick beta6 release.
Comment #13
drugan commentedFixed.
Comment #15
sekoz commentedCould you reopen this issue please?
I just upgraded to commerce 2.16 with commerce_xquantity 1.0.0-beta6.
Now I need to change the quantity in the cart and press the "update cart" again to get the volume discount applied to the unit price.
Just pressing "update cart" will not apply the discount, I need to change the quantity in the cart first.
Comment #16
drugan commented@sekoz
Thank you for the report. I'll look into it a bit later.
Comment #17
sekoz commentedI am also getting a flood of notices:
Message Notice: Indirect modification of overloaded property Drupal\commerce_xquantity\Form\XquantityAddTocartForm::$quantityPrices has no effect in Drupal\commerce_xquantity\Entity\XquantityOrderItem->setQuantityPrices() (line 143 of web/modules/contrib/commerce_xquantity/src/Entity/XquantityOrderItem.php)
Comment #18
drugan commented@sekoz
What is your
PHPandDrupalandDrupal Commerceversions?Comment #19
sekoz commentedPHP 7.2.24
Drupal core 8.8.0
Drupal Commerce 8.x-2.16
Comment #20
drugan commentedOkey, thank you. I'll try to reproduce it....
Comment #21
drugan commentedSeems that now properties of a class should be explicitly declared before used.
Please try the patch if that works for you.
Comment #22
sekoz commentedThanks that patch works great.
Prices are updating correctly and no more notices flood.
Comment #24
drugan commentedI think I'll move it to the next beta7 release right now.