- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2018
09:37 AM
07-23-2018
09:37 AM
Script to update products
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; }
Labels:
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2018
01:19 AM
07-28-2018
01:19 AM
Re: Script to update products
Can you please help me with this topic???
thanks
Alex
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2018
02:34 AM
08-02-2018
02:34 AM
Re: Script to update products
this script work fine? or you need help because got an error? and if you got an error, can you post log file?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2018
03:16 AM
08-02-2018
03:16 AM
Re: Script to update products
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.
Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2019
08:31 AM
02-28-2019
08:31 AM
Re: Script to update products
I was off the office for a long period ... Sorry for my delay in thanking you. You have been very kind and I'm sure the solution will certainly work.