[5.x] Trigger event to override inventory locations per order#4276
[5.x] Trigger event to override inventory locations per order#4276yoannisj wants to merge 4 commits into
Conversation
- trigger event when getting the inventory locations for given purchasable `craft\commerce\services\InventoryLocations:: EVENT_REGISTER_INVENTORY_LOCATIONS_FOR_PURCHASABLE ` which allows modules and plugins to select a subset of the store’s inventory locations based on the order details - add first argument `$order` to \craft\commerce\base\Purchasable::getStock()` to aggregate stock in order-specific inventory locations only - add first argument `$order` to \craft\commerce\base\Purchasable::hasStock()` to check for stock in order-specific inventory locations only - verify stock available in order-specific inventory locations when checking a purchasable’s availability
|
I just discovered an error in this PR's code, and will push a fix later today. |
|
@lukeholder is there any chance to get this released in the next version? Our project is relying on this event listener and we would need to know if this is coming or not. |
- Fix RegisterInventoryLocationsForPurchasableEvent docblock, @SInCE, @Property type, dead instanceof check, and trailing newline - Update the event docblock example on InventoryLocations to reference the correct class and constant, and use the setter - Match the constant value to its name (registerInventoryLocationsForPurchasable) - Skip event construction when no handlers are attached - Restore brace style and remove duplicate $order assignment in Purchasable::populateLineItem - Drop the unused $order parameter from Inventory::getInventoryItemByPurchasable and stray blank line - Normalize Order|null to ?Order, fix @SInCE to 5.6.5 on new symbols
|
Hi @yoannisj — really appreciate this, it potentially will unlock #4273/#4247. I pushed a follow-up commit (ab492c8) cleaning up the event class, docblocks, brace style, the dead Before we merge there are a few design-level questions I'd love your take on, since they could cause subtle issues in production:
1, 3, and 5 feel most important to address before this goes out. I also think it should probably be in a major version, not in the next release. Let me know your thoughts and I'm happy to help land any of the remaining changes. |
Description
This PR adds the following changes in order to allow modules and plugins to select a subset of the store's inventory locations, when Craft-Commerce is accessing a given purchasable's stock. When this is done in the context of an order, then this selection can be made based on the order's details.
craft\commerce\services\InventoryLocations:: EVENT_REGISTER_INVENTORY_LOCATIONS_FOR_PURCHASABLEwhen accessing a given purchasable's stock, which allows modules and plugins to select a subset of the store’s inventory locations$orderto \craft\commerce\base\Purchasable::getStock()` to aggregate stock in order-specific inventory locations only$orderto \craft\commerce\base\Purchasable::hasStock()` to check for stock in order-specific inventory locations onlyRelated issues
The changes make it possible for modules or plugins to implement the use-cases described in #4273 and most use-cases from #4247 .