It has been some time since the new version-Magento 2 was released. However, there’re still quite many issues that haven’t been optimized until this moment. Typically, importing products is one of these problems.
To make an example, we will proceed importing a CSV file with the following format (m2-import.csv):
– Fill in all required fields on CSV file
– Make sure all SKU is unique
– Format the image file path correctly
To import a large number of products at the same time, URL KEY field needs to be filled.
To have the URL KEY format compatible with Magento, we need to write script in order to reformat the product name, following is a reference script
Upload file m2-import.csv to root file in magento
Create urlkey.php file, then paste the following script:
$urlArray = array();
if (($handle = fopen("m2-import.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $data[8]); // 8 is position of column "name"
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", '-', $clean);
$i = 0;
if(in_array($clean, $urlArray)){
$clean = $clean . '-1';
$urlArray[] = $clean;
}else{
$urlArray[] = $clean;
}
}
fclose($handle);
}
foreach ($urlEnd2 as $value) {
echo $value . '';
} print_r(array_count_values($urlEnd2));
?>
Once all needed information is completely filled in CSV file –> proceed importing
Go to Admin –> System –> Import
After completing import, remember to reindex data and upgrade website database.
Reindex: php bin/magento indexer:reindex
Upgrade: php bin/magento setup:upgrade
When things are done, reload the site and check the result.