Jump to content

Update product attribute quantity from command line


Recommended Posts

I have created a module in Prestashop 1.7.6 which update products + product attribute stocks by 2 ways :

  • In the back office (the settings of the module) it works perfectly
  • From a cron by the server every night using a declared service called by this kind of command line :
php /var/www/MyPrestashopSite/bin/console mymodule:update updateProducts --no-debug

For many other services that I have created and I use it works perfectly, but for this one, I use the "new" stock management in Prestashop, and when I use the \StockAvailable methods, it always call the Context and trigger an error of StockManagementRepository line 130 :

'Determining the active language requires a contextual employee instance.'

This error prevent my module from updating product + product attributes stock out of Context.

In my code I tried to use the following methods, to get and modify the stock in the cleanest possible way :

\StockAvailable::getQuantityAvailableByProduct(...)

\StockAvailable::setQuantity(...)

\StockAvailable::updateQuantity(...)

Note that I force the $shopId to 1 to avoid getting the Context as much as possible.

Do you know how to update a stock in a clean way (not just modifying the stocks, product attributes and products tables) in Prestashop from a cron (out of the context and without employee connexion), and without calling the Prestashop webservice ?

Thank you.

Link to comment
Share on other sites

  • 5 months later...
  • 1 month later...

Yes, we got the same error.

In front it is OK that Presta requires a contextual employee instance.

But for bulk/custom PHP file where You include core functionality like

include(dirname(__FILE__).'/config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');

and want to update product quantity something like this

$product = new Product(ITEM_ID);
StockAvailable::setQuantity(PRODUCT_ID, (int)$id_product_attribute, (int)QUANTITY, SHOP_ID);

I just simply offer to add error reporting 0 at the beginning of Your custom file

error_reporting(0);

and setting new quantity will work just fine.

Why this way? Cause in bulk/plain/custom file there is no way active language.

 

It is recommended to setQuantity in cycle where cycle items will be shops ids to set quantity same for all shops.

Link to comment
Share on other sites

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...