If you using console command for this propose that is very fast an useful things. For console command you need to do this below code. In between you have to put the product update csv in var/upload/ folder or you select other folder.
- add below code in di.xml file in app\code[Vendor][Namespace]\etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="bulk_product_update" xsi:type="object">[Vendor]\[Namespace]\Console\Command\UpdateProduct</item>
</argument>
</arguments>
</type>
</config>
- Create UpdateProduct.php in [Vendor][Namespace]\Console\Command
<?php
namespace [Vendor]\[Namespace]\Console\Command;
use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;
class UpdateProduct extends Command
{
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ResourceModel\Product\Action $productAction,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
\Magento\Framework\File\Csv $csv,
\Magento\Framework\Filesystem\Driver\File $fileDriver,
\Magento\Framework\Filesystem\DirectoryList $dir,
\Magento\Framework\Filesystem\Io\File $file
)
{
$this->storeManager = $storeManager;
$this->productAction = $productAction;
$this->productFactory = $productFactory;
$this->stockRegistry = $stockRegistry;
$this->csv = $csv;
$this->fileDriver = $fileDriver;
$this->dir = $dir;
$this->file = $file;
parent::__construct();
}
protected function configure()
{
$this->setName('bulk:productupdate')
->setDescription('Bulk product update with stock & Images');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$csvFilePath = $this->dir->getPath('var').'/upload/product_data.csv';
if (!$this->fileDriver->isExists($csvFilePath)) {
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid file upload attempt.'));
}
$storeIds = array_keys($this->storeManager->getStores());
$csvData = $this->csv->getData($csvFilePath);
$attributeArray = array();
foreach ($csvData as $row => $data) {
if ($row > 0){
$product = $this->productFactory->create()->load($data[$attributeArray['id']]);
if(isset($data[$attributeArray['qty']])) {
$stockData = array(
'use_config_manage_stock' => $data[$attributeArray['use_config_manage_stock']],
'manage_stock' => $data[$attributeArray['manage_stock']],
'is_in_stock' => $data[$attributeArray['is_in_stock']],
'qty' => $data[$attributeArray['qty']]
);
$stockItem = $this->stockRegistry->getStockItem($data[$attributeArray['id']]);
$stockItem->setData($stockData);
$stockItem->save();
}
$updateAttributes['sku'] = $data[$attributeArray['sku']];
$updateAttributes['name'] = $data[$attributeArray['name']];
$updateAttributes['status'] = $data[$attributeArray['status']];
$updateAttributes['weight'] = $data[$attributeArray['weight']];
$updateAttributes['visibility'] = $data[$attributeArray['visibility']];
$updateAttributes['price'] = $data[$attributeArray['price']];
foreach ($storeIds as $storeId) {
$this->productAction->updateAttributes([$data[$attributeArray['id']]], $updateAttributes, $storeId);
}
if (isset($data[$attributeArray['image']])) {
$imageUrl = $data[$attributeArray['image']];
$newFileName = $this->dir->getPath('media'). '/tmp/' . baseName($imageUrl);
$result = $this->file->read($imageUrl, $newFileName);
if ($result) {
$_product->addImageToMediaGallery($newFileName, array('small','thumbnail','base'), true, false);
}
}
$product->save();
$output->writeln('Update Complete Product: '.$data[$attributeArray['sku']]);
}
} else {
foreach($data as $key => $dataValue) {
$attributeArray[$dataValue] = $key;
}
}
}
$output->writeln('All product Update complete');
}
}
At last you need to run in terminal below command:
bin/magento bulk:productupdate