How To Update Bulk Product SKU Using CSV In Magento 2
The product SKU is a unique id for each product in a Magento 2 store. Changing product SKU in bulk is not easy in Magento 2.
However, sometime, it may happen that you need to update bulk product SKU in Magento 2 when you are importing products from a third-party platform.
The below code is the solution for the same.
You can use this code to update product SKU, product price, etc. in bulk in Magento 2 store using the CSV file.
Method to update bulk product SKU using CSV in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php use Magento\Framework\AppInterface; try { require_once __DIR__ . '/app/bootstrap.php'; } catch (\Exception $e) { echo 'Autoload error: ' . $e->getMessage(); exit(1); } try { $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $objectManager->get('Magento\Framework\App\State')->setAreaCode('adminhtml'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager $row = 1; $csvName = "{file.csv}"; // Here you need to place .csv file path $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository'); if ( ($handle = fopen($csvName, "r")) !== FALSE ) { $row = 1; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ( $row == 1 ) { $row++; continue; } // to skip first line if first row is column name. $row++; try { $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository'); $sku = $data[0]; // this can manage as per your csv. $productObj = $productRepository->get($sku); $productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface'); $productRepository->save($productObj); } catch (\Exception $e) { echo $e->getMessage(); } } fclose($handle); } } catch (\Exception $e) { echo $e->getMessage(); } |
Any doubts about the implementation? Please mention them in the Comments section below. I’d be happy to help you out.
Do share the post with fellow developers via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
Hi Sanjay
Thanks for this article.
However, it is not working when we have products with tier prices and multiple attribute sets. Can you share ideas on how to resolve it if you already know?
$productObj = $productRepository->get($sku);
Hello Vivek,
Kindly check the code again as it is working properly from our end.
Thank You
Could you please share sample CSV will be much appreciated. Is this script will update SKU in Old orders, Old invoices, Old shipments
Is this script will work with if i want to update SKU’s from 12 digit to 13 only for simple products and this script will update the SKu number everywhere like Simple Products, Configurable Products, Orders, Old Orders, Invoices, Shipments
Hello Gagandeep,
The SKU will not change on old orders, invoice as order/invoice gets generated before changing the SKU.
Plus, the above code is for one SKU related example only. That’s why, only one column has been there in CSV.
Thank You
will the above script update all the child simple product sku that are associated with config products?
Hello,
The products’ SKUs that are in the CSV, those products will be updated no matter the type of products.
Thank you.
Hi,
We are looking for the script which will update SKUs (from / to _) for Magento 2. Do you have a module for this? Or step to step guide for the same.
Hey Doorita, you just need to modify the script given in the above post as per your requirements.
Thank you