{"id":934,"date":"2020-04-23T09:33:07","date_gmt":"2020-04-23T09:33:07","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/04\/23\/update-bulk-product-sku-using-csv-in-magento-2\/"},"modified":"2025-05-22T13:27:04","modified_gmt":"2025-05-22T07:57:04","slug":"update-bulk-product-sku-using-csv-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/update-bulk-product-sku-using-csv-in-magento-2\/","title":{"rendered":"How To Update Bulk Product SKU Using CSV In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, sometime, it may happen that you need to&nbsp;<em><strong>update bulk product SKU in Magento 2<\/strong><\/em>&nbsp;when you are importing products from a third-party platform.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below code is the solution for the same.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use this code to update product SKU, product price, etc. in bulk in Magento 2 store using the CSV file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to update bulk product SKU using CSV in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nuse Magento\\Framework\\AppInterface;\ntry {\n    require_once __DIR__ . '\/app\/bootstrap.php';\n\n} catch (\\Exception $e) {\n    echo 'Autoload error: ' . $e->getMessage();\n    exit(1);\n}\n\ntry {\n    $bootstrap = \\Magento\\Framework\\App\\Bootstrap::create(BP, $_SERVER);\n    $objectManager = $bootstrap->getObjectManager();\n    $objectManager->get('Magento\\Framework\\App\\State')->setAreaCode('adminhtml');\n\n    $objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance(); \/\/ Instance of object manager\n\n    $row = 1;\n    $csvName = \"{file.csv}\"; \/\/ Here you need to place .csv file path\n    $productRepository = $objectManager->get('\\Magento\\Catalog\\Model\\ProductRepository');\n    if ( ($handle = fopen($csvName, \"r\")) !== FALSE ) {\n        $row = 1;\n        while (($data = fgetcsv($handle, 1000, \",\")) !== FALSE) {\n            if ( $row == 1 ) {\n                $row++;\n                continue;\n            } \/\/ to skip first line if first row is column name.\n            $row++;\n            try {\n                $productRepository = $objectManager->get('\\Magento\\Catalog\\Model\\ProductRepository');\n\n                $sku = $data[0]; \/\/ this can manage as per your csv.\n\n                $productObj = $productRepository->get($sku);\n\n                $productRepository = $objectManager->create('Magento\\Catalog\\Api\\ProductRepositoryInterface');\n                $productRepository->save($productObj);\n\n\n            } catch (\\Exception $e) {\n                echo $e->getMessage();\n            }\n        }\n        fclose($handle);\n    }\n} catch (\\Exception $e) {\n    echo $e->getMessage();\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the post with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-934","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=934"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/934\/revisions"}],"predecessor-version":[{"id":14953,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/934\/revisions\/14953"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}