Needs review
Project:
Commerce Stock
Version:
3.x-dev
Component:
Miscellaneous
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
4 Sep 2020 at 00:01 UTC
Updated:
24 Jun 2026 at 15:36 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
guy_schneerson commentedOnly one of those should be able to complete checkout. It is not uncommon for people to add an item in their basket and never come back to the site and you do not want that item to become unavailable as a result.
Comment #3
d2d commentedThank you. That clears things up. I will try to convince the site owner that everything is OK. She may not be happy, but as you point out, the alternative is worse.
Comment #6
krystalcode commentedI had a similar requirement recently.
I have created an MR that does the following:
- Configuration option to determine whether stock should be reserved upon order placement or when adding products to cart.
- Two new transaction types: Reservation and Release.
- When products are added to the cart, or order items are updated/deleted, stock is reserved or released accordingly.
- When an abandoned cart is deleted (e.g. through the delete abandoned carts feature) stock is released.
- When the order is placed stock reservations are converted to stock sales.
No tests written yet.
It does not address the concern that the stock may be reserved and then users abandon carts; the stock will stay reserved till the abandoned cart is deleted. But it's a starting point, I think we can find a solution about that as well.
What if the stock reservation is cancelled after the user session ends? Or after a configurable amount of time. We may need a 2nd quantity field on the order item so that desired quantity is not lost in that case.
Comment #7
jason ruyle commentedI generated a patch from your branch 3169030-decrement-available-quantity
I did run into issues on the checkout process, after I submit from "Pay and complete", I get this error:
In terms of removing items from the cart, couldn't the store owners set the time for abandoned carts to something a lot lower (like 30 minutes). Then on cronjobs, those carts would be set to abandoned and at that stage, the stock would be removed?
Comment #8
krystalcode commentedThanks for testing.
Line 133:
foreach ($event->getOrderItems() as $item) {should beforeach ($order->getOrderItems() as $item) {Comment #9
jason ruyle commentedI still got the same error with changing just event to order.
I did a quick test with the following on line 133.
to:
And there was no error.
I checked the commerce_stock_transaction table and the transaction id is being logged properly. Changes from 10 (STOCK_RELEASE) to 4 (STOCK_SALE).
Comment #10
jason ruyle commentedHere is the patch from #9.
Comment #11
damienmckennaShould it instead double-check the inventory as part of the checkout process? On one site I'm involved in they regularly have people add items to their cart and leave them there for several days; this change could potentially stop would prevent others from completing their transaction.
While I do appreciate the idea of a customer walking around in a store with an item in their cart they haven't bought yet, in this analogy the customer wouldn't continue walking around the store for two weeks deciding whether they wanted to buy it.
Many sites that have tight inventory controls will have a timer that requires a visitor to complete the checkout within a certain amount of time, otherwise the item is made available for others. This is common with event ticket sales, e.g. concerts, etc.
Comment #12
damienmckennaI opened a separate issue for my idea from #11: #3347485: Double-check inventory when checkout started
Comment #13
krystalcode commentedThis is optional. If that is not the requirement for your site, do not configure it to reserve the item when it is added to the cart.
Comment #14
gidarai commentedRerolled the patch from #10 and added numerous fixes.
- The 'stock reservation upon adding to cart' feature is now fully optional
- Patch now works with the latest version
- Improved stock form functionality
- Added support for stock enforcement
My colleague (@arantxio.gottmers) and i have been working on this as we need it for our site and thought it would be beneficial to share it here as well.
Comment #15
arantxioI've noticed that it missed a import, so here is the adjusted patch.
Comment #16
gidarai commentedThis patch also works for version 3.x, so i'll up the target version. Can this be committed if there's nothing holding it back?
We have been using this patch for our production site for about a few months now without any issues.
Comment #17
guy_schneerson commentedAmazing work on this.
I am happy to add this feature, although it is not a complete solution as stock does not support the expression of the cart and does not have a dependency on a module that can.
But as it's optional, I think we can add it (I may reword it a bit but that's not a blocking issue).
I will, however, only commit new features to the 3.x branch.
As this is a significant update, I will need to review it properly before committing. So far all looks good and works as expected.
Comment #18
starlight-sparkleI've observed what seems like a possible issue with the design of patch #14/#15.
The implementation of stock enforcement in the patch doesn't seem to account for offsite payment flows, which need to lock the cart and then render the checkout payment process pane in its own dedicated checkout step. Attempting to make offsite payment with this patch results in customers being returned to the
/cartpage and being prevented from proceeding with checkout.The patched
commerce_stock_enforcement_is_order_in_stock()function assumes that the current user's reserved quantity can be reliably calculated viacommerce_stock_enforcement_get_ordered_quantity(), which in turn retrieves cart quantities by callingCartProvider::getCarts().However,
CartProvider::getCarts()only includes unlocked carts, so when you proceed to the payment checkout step using an offsite payment method, the cart you're trying to make payment for is locked, and stops being counted in the "reserved quantity", thus you get redirected back to cart and can never complete payment.commerce_stock_enforcement_is_order_in_stock()probably needs to calculate reserved quantity by some other way.Comment #22
starlight-sparkleAdded a query condition to
LocalStockChecker::getLocationStockTransactionLatest()to exclude "reservation" transactions if called fromLocalStockChecker::getTotalStockLevel()and not if called fromLocalStockChecker::getTotalAvailableStockLevel().Comment #23
starlight-sparkleJust noticed that this has since become a feature request, not a support request.
Reviewed with some other devs in an external platform; it makes more sense to update
commerce_stock_enforcement_get_ordered_quantity()to use an entity query to retrieve carts including locked carts.