Hello Devs,
I am doing some Stock recalculations for a particular kind of products. That is, implementing my own StockUpdater.
I am facing dificulties in two situations.
- Can’t update the available stock when the Stock is manually updated by the shop owner.
- Can’t update the available stock when an order is deleted before it has been completed.
Lets see the events listened to in the Shopware Stock updater:
// SW StockUpdater
return [
CheckoutOrderPlacedEvent::class => 'orderPlaced',
StateMachineTransitionEvent::class => 'stateChanged',
PreWriteValidationEvent::class => 'triggerChangeSet',
OrderEvents::ORDER_LINE_ITEM_WRITTEN_EVENT => 'lineItemWritten',
OrderEvents::ORDER_LINE_ITEM_DELETED_EVENT => 'lineItemWritten',
];
orderPlaced : updates the available stock (open orders - stock).
No problem with implementing my own logic.
stateChanged : listens to the order.state machine. Updates stock & available stock when the order state changes between [open, in_progress, completed, cancel]
No problem with implementing my own logic.
Here begins the strange part (at least for me):
lineItemWritten: : I do not clearly understand what it does: it is triggered when i buy a product but is skipping due to the firs condition in the foreach loop.
if ($result->getOperation() === EntityWriteResult::OPERATION_INSERT) { continue; }
As I understand from the comment above (‘If the product of an order item changed, the stocks of the old product and the new product must be updated.’)
it is triggered when you change a Product directly in the Order, then it updates the Stock. I am still working with 6.3.1.1 where this functionality is not imlemented, hence did not tested it.
triggerChangeSet :
This is for me the most dificult one to understand.
Let me try: The DAL is working with a CommandQueue. The Command class contains all necessary information to build, execute and build an EntityWriteResult, which contains all the information for the response i guess.
Now, some of the Commands implement the ChangeSetAwareInterface, namely the DeleteCommand and UpdateCommand. As far as i understand, you can tell these commands to get the state of your Entity before the change and add it to the Response. Therefore in the triggerChangeSet
the the delete, update commants are marked to do so, with the$command->requestChangeSet();
method.
But all this still does not help me to understand when and where the update & updateAvailableStock method in the StockUpdater is called when the user manually updated the stock. I know its called, because i can log it. But i dont understand where it is triggered so i can change the behaviour of the stock & availableStock calculation.