Jump to content

How to mass update the stock quantity of a batch of products in PrestaShop 1.7.6?


Recommended Posts

I work in the integration of an ERP (Random) and PrestaShop, one of the points considered by the integration is the stock level of the products. When there are movements in the stock of the products in the ERP originating outside of PrestaShop (for example, transfers between warehouses, purchases, external sales, etc.), these stock variations must be synchronized with the physical stock (physical_stock) of PrestaShop, because the reserved stock (stock_reserved) exists only in the PrestaShop context and is related to the reserved products in orders that have not yet been dispatched, while the available stock (stock_available) does not consider the reserved stock by design, then if the Stock that comes from the ERP is assigned to the available stock, the stock reserved until now would double. On the other hand, the physical stock considers both the available stock and the reserved stock, then the stock that comes from the ERP is assigned to the physical stock, the reserved stock is maintained and the available stock will vary depending on the following relation:

stock_available.quantity = stock_available.physical_quantity - stock_available.reserved_quantity

This is currently working correctly in production, however, what I am looking for is to design these operations in the best way through a PrestaShop module, which through a FrontController receives an ajax request where one of the request parameters (Request->_ POST['stock_quantity_changes']) is an input in array format, for example:

(array) [ $id_stock_avaiblable_1 => $new_quantity_1, $id_stock_avaiblable_2 => $new_quantity_2, ... ]

with which I would like to update the set of stocks that it contains in a single operation. So far, I found method src/PrestaShopBundle/Entity/Repository/StockRepository.php->bulkUpdateStock (https://github.com/PrestaShop/PrestaShop/blob/100c5c028b05c579f5788a036041e45cef510e1d/src/PrestaShopBundle/Entity/Repository/StockRepository.php#L102), that would allow me to do what I need, however I have not figured out how to create a proper instance of this class (StockRepository) yet. The StockRepository-> bulkUpdateStock method receives a set of stock movements (MovementsCollection) and with it, updates a set of available stocks, both in individual products and in pack's if necessary, in addition, records the stock movements (StockMvt's), executes the actionUpdateQuantity hook for each modified stock and removes the value of each modified stock from the cache, all this can be checked in /src/Core/Stock/StockManager.php->updateQuantity (https://github.com/PrestaShop/PrestaShop/blob/100c5c028b05c579f5788a036041e45cef510e1d/src/Core/Stock/StockManager.php#L153). Finally the method bulkUpdateStock synchronize the level of physical stock with the available stock + reserved stock. I am interested in using all this logic when updating a set of stock to adapt it to PrestaShop's operation, since currently only updating the values of the stock_available table I am not registering the StockMvt's, cleaning the cache or executing the actionUpdateQuantity hook.

In summary, I found the StockRepository class that contains the bulkUpdateStock method that would allow me to correctly update the levels of a set of stocks in a single operation, however, to instantiate this class requires a set of parameters, which can be verified in https://github.com/PrestaShop/PrestaShop/blob/100c5c028b05c579f5788a036041e45cef510e1d/src/PrestaShopBundle/Entity/Repository/StockRepository.php#L72, and so far I have not found any instantiation examples for this class in the source code, I have only found code that uses this class already instantiated, as you can check in src/PrestaShopBundle/Controller/Api/StockController.php (https://github.com/PrestaShop/PrestaShop/blob/100c5c028b05c579f5788a036041e45cef510e1d/src/PrestaShopBundle/Controller/Api/StockController.php#L126). So the question finally is, how can I properly instantiate the StockRepository class ?, or is there any alternative to do what I need ?, regards :)

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...