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=$this->getLoadProduct($csv[$i][0]);
$product->setPrice($csv[$i][1]);
$product->setStockData(['qty' => $csv[$i][2], 'is_in_stock' => 1]);
$product->setQuantityAndStockStatus(['qty' => $csv[$i][2], 'is_in_stock' => 1]);
$product->save();
}
}
phtml:
$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?