I create a module for updating price and stock for product from csv file from the frontend. Here is my code :

Block :

public function getLoadProduct($id)
    return $product = $this->productRepository->getById($id, true);

public function changePriceById($csv){
    for ($i=0; $i < count($csv); $i++) {

        $product->setStockData(['qty' => $csv[$i][2], 'is_in_stock' => 1]);
        $product->setQuantityAndStockStatus(['qty' => $csv[$i][2], 'is_in_stock' => 1]);


$product = $this->changePriceById($csv3);

It's works for me each i reload the page, but what is the best time to update the product price and stock?

Is it each time product get called to the page?

Because it's need much time to update hundred till thousand of product. And one more thing, is it ok to place the code to block and not to a controller?


If I was in your place, I did like this

  1. Create custom table by setup scripts
  2. Whenever import csv from front-end, save the data into custom tables.
  3. Create custom cron job, every one minute(time depends on your need) that will scan table for the new record or If you don't want to create custom table, directly scan the new csv file in uploaded path. If it found new record, It will update price & stock.

In this approach, you don't need to wait long in front-end, Cron will take care of the update.


I'd do this by CRON job once in a day at midnight(least traffic on the website) if I were at your place. Price and stock are something which you wouldn't want to update every minute so once in a day is perfect. You can add logs as well in the script to trace the import statistics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.