How to Update Bulk Product SKU using CSV in Magento 2

Magento 2 does not make it easy to change SKUs in bulk. Try to do it via the import/export or dataflow profiles and you run into all kinds of problems. There is, however, a fairly easy way to do it and it simply involves adding a php script to your server and a CSV file of before and after SKUs. In this post we’ll let you know How to Update Bulk Produt SKU using CSV in Magento 2.

Note: Be sure to test this with only one or two product SKUs before doing it with all your SKUs. Also, be sure to backup your database before attempting this.

Step 1: Create sku folder on Magento 2 Root directory.

Step 2: Create sku2sku.csv CSV File on sku folder with Before and After SKUs In the first column, list your current SKUs and in the second column list the new SKUs.

Do not include headings in your CSV file.

Be sure this file is saved as a CSV file in the UTF-8 or ANSI encoding. You might run into problems with this if you create the file using Excel.

Step 3: Upload the CSV file on the sku folder on your server so that it’s path is Magento 2 Root directory/sku/sku2sku.csv.

Step 4: Create the index.php file at Magento 2 root directory/sku/index.php.

And paste the following code into index.php and save the file.

<?php ini_set('memory_limit', '512M'); set_time_limit(0); require __DIR__ . '../../app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $app = $bootstrap->createApplication('changeskus'); $bootstrap->run($app);

Step 5: Create the changeskus.php file at Magento 2 root directory/sku/changeskus.php.

And paste the following php code into changeskus.php and save the file.

<?php class changeskus extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface { public function launch() { $this->_state->setAreaCode('frontend'); //Set area code 'frontend' or 'adminhtml $updates_file="sku2sku.csv"; $sku_entry=array(); $updates_handle=fopen($updates_file, 'r'); if($updates_handle) { while($sku_entry=fgetcsv($updates_handle, 1000, ",")) { $old_sku=$sku_entry[0]; $new_sku=$sku_entry[1]; echo "<br>Updating ".$old_sku." to ".$new_sku." - "; $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->loadByAttribute('sku', $old_sku); try { if ($_product) { $_product->setSku($new_sku)->save(); echo "successful"; } else { echo "item not found"; } } catch (\Exception $e) { echo "Cannot retrieve products from Magento: ".$e->getMessage()."<br>"; return; } } } fclose($updates_handle); return $this->_response; } public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception) { return false; } }

Inside changeskus.php, CreateApplication method comes from a bootstrap class that’s most important part of Magento 2 initiate application. It creates an instance of an application class. createApplication expects an implementation of the \Magento\Framework\AppInterface.

Lunch() method is called by \Magento\Framework\App\Bootstrap::run and this is main part that initiate Magento 2 environment. And finally called $response = $application->launch();

For more information on how to Magento 2 application initialization. You can refer below link to find more information.

Step 6: Run the Script To run the script simply use your internet browser and navigate to If you have a multi-site setup use the master or primary site as set by your hosting provider.

When the page opens you should see confirmation messages that your SKUs were updated. Your SKUs should now be successfully updated.

If you’re finished updating the SKUs, remove the CSV and PHP files that you added to the server.

Step 7: Errors If you run into the following error, don’t worry too much. Just re-run the script and see if more SKUs get updated.

Cannot retrieve products from Magento: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction

If you have a lot of SKUs to update you can expect the script to take several minutes at least, to complete.

I hope this Magento technical post helped you to Update SKU using CSV in Magento 2. Bookmark it for your future reference. Do comment below if you have any other questions or doubts.

P.S. Do share this note with your team.

Review other articles maybe it'll help you too.

Recent Articles
Chrome Extension
Copyright © 2024 All rights reserved.
Ads OFF toggle_off