I am writing a module that uses a cli command get a csv file and use that to change the prices of a selection of products based on sku and new price retrieved from an CSV file.
I have the logic for the CLI command, for reading the csv and getting a collection of products based on the the sku's. But I would like to en mass change the prices for all the products.
How I now have it working now is that my module extends the command class and adds a cli command for this specific situation, bin/magento import:somename:pricingcsv path/to/file.csv
. I use Magento\Framework\File\Csv
class to read the csv get the sku's and prices, get a product collection, loop over every product in the collection set the new price and save them individually, but seeing as there are over 5000 products this method takes almost a second per product so over 1.5 hours in total.
$productCollection = $this->productCollectionFactory->create();
$productCollection->addFieldToSelect('*'); // only need sku, price, id,
$productCollection->addAttributeToFilter('sku', ['in' => array_keys($productsArray)]);
/** @var Product $product */
foreach($productCollection as $product) {
/** @var Action */ // injected using the constructor
$this->productAction->updateAttributes([$product->getId()], ['price' => $newPrice], 0);
Even though the price is the only property that is changing, saving the product object will go over all the product properties and update everyone even though only the price has changed.
I imagine that has to be a more streamlined way to do this. I have been looking through the