Hello
I'm try to update Magento products and stock quantity at the same time from a CVS.
I'm able to update stock quantity by the following script but I need to update product attributes (barcode), as well. Who's can help me updating the piece of code missed to accomplish this?
Any contribution to improve the script is welcome!
Thanks
Alex
<?php /*************************************************** ** PHP SCRIPT - tested on Magento 2.2.5 - AlexF ** Update Stock Quantity from a Three Columns CSV file ** CSV file should be created with Header and Rows ** as follow: ** "sku","qty","barcode" ** "AAAAA",10.0000,"123456789012" ** "BBBBB",130.0000,"123456789012" ***************************************************/ use Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $_objectManager = $bootstrap->getObjectManager(); $state = $_objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml'); //Load Array from CSV $csvfilename="stockdata.csv"; //path & name to CSV file $products = Array(); $products = parse_csv_file($csvfilename); //Process all SKU from Array foreach ($products as $value) { try{ $stockRegistry = $_objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface'); $stockItem = $stockRegistry->getStockItemBySku($value['sku']); $stockItem->setQty($value['qty']); $stockItem->setisInStock($value['qty'] > 0 ? 1:0); // $stockItem->setQuantityAndStockStatus(['qty' => $value['qty'], 'is_in_stock' =>($value['qty'] > 0 ? 1 : 0)]); $stockRegistry->updateStockItemBySku($value['sku'], $stockItem); //Display Operation Done print_r("Updated SKU " . trim($value['sku']) . " to " . $value['qty'] . "\n"); } catch(Exception $e){ print_r("SKU " . trim($value['sku']) . " not found into Magento2 Product Archive "."\n"); }; }; exit("Finished."); //function to load CSV file function parse_csv_file($csvfile) { $csv = Array(); $rowcount = 0; if (($handle = fopen($csvfile, "r")) !== FALSE) { $max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000; $header = fgetcsv($handle, $max_line_length,";"); $header_colcount = count($header); while (($row = fgetcsv($handle, $max_line_length,";")) !== FALSE) { $row_colcount = count($row); if ($row_colcount == $header_colcount) { $entry = array_combine($header, $row); $csv[] = $entry; } else { error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount"); return null; } $rowcount++; } //echo "Totally $rowcount rows found\n"; fclose($handle); } else { error_log("csvreader: Could not read CSV \"$csvfilename\""); return null; } return $csv; }
Can you please help me with this topic???
thanks
Alex
this script work fine? or you need help because got an error? and if you got an error, can you post log file?
Hello @Alex70
please check below script
<?php /*************************************************** ** PHP SCRIPT - tested on Magento 2.2.5 - AlexF ** Update Stock Quantity from a Three Columns CSV file ** CSV file should be created with Header and Rows ** as follow: ** "sku","qty","barcode" ** "AAAAA",10.0000,"123456789012" ** "BBBBB",130.0000,"123456789012" ***************************************************/ use Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $_objectManager = $bootstrap->getObjectManager(); $state = $_objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('adminhtml'); //Load Array from CSV $csvfilename="stockdata.csv"; //path & name to CSV file $products = Array(); $products = parse_csv_file($csvfilename); //Process all SKU from Array foreach ($products as $value) { try{ $stockRegistry = $_objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface'); $stockItem = $stockRegistry->getStockItemBySku($value['sku']); $stockItem->setQty($value['qty']); $stockItem->setisInStock($value['qty'] > 0 ? 1:0); // $stockItem->setQuantityAndStockStatus(['qty' => $value['qty'], 'is_in_stock' =>($value['qty'] > 0 ? 1 : 0)]); $stockRegistry->updateStockItemBySku($value['sku'], $stockItem); //Display Operation Done print_r("Updated SKU " . trim($value['sku']) . " to " . $value['qty'] . "\n"); $productRepository = $_objectManager->get('\Magento\Catalog\Model\ProductRepository'); $product = $productRepository->get($value['sku']); $product->setBarCode('barcode value'); // here you need to change based on attribute $this->_productRepository->save($product); } catch(Exception $e){ print_r("SKU " . trim($value['sku']) . " not found into Magento2 Product Archive "."\n"); }; }; exit("Finished."); //function to load CSV file function parse_csv_file($csvfile) { $csv = Array(); $rowcount = 0; if (($handle = fopen($csvfile, "r")) !== FALSE) { $max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000; $header = fgetcsv($handle, $max_line_length,";"); $header_colcount = count($header); while (($row = fgetcsv($handle, $max_line_length,";")) !== FALSE) { $row_colcount = count($row); if ($row_colcount == $header_colcount) { $entry = array_combine($header, $row); $csv[] = $entry; } else { error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount"); return null; } $rowcount++; } //echo "Totally $rowcount rows found\n"; fclose($handle); } else { error_log("csvreader: Could not read CSV \"$csvfilename\""); return null; } return $csv; }
If works then mark as solution.