This repository was archived by the owner on Apr 29, 2019. It is now read-only.
This repository was archived by the owner on Apr 29, 2019. It is now read-only.
CatalogImportExport categoryProcessor is using default store id values #88
Closed
Description
From @koenner01 on October 13, 2016 13:5
When importing product data through the catalogImportExport module, new categories will be created even though categories with the correct path already exist.
Preconditions
- Magento 2.1
- PHP7.0
Steps to reproduce
- Create a category 'Test'
- Set the category name to something completely different on default storeview
- Create a csv with catalog product data
- Set 'Default Category/Test' as the 'categories' value
- Import
Expected result
- The product is added to the existing category 'Test' (which we just created)
Actual result
- The product will not be added to the existing category
- A new category with the exact same path is created and the product is put in the new category
OR - An error is thrown with message "1. Category "Default Category/Test" has not been created. URL key for specified store already exists. in row(s): 1"
I traced this issue back to \Magento\CatalogImportExport\Model\Import\Product\CategoryProcessor.
In the initCategories function a category collection is loaded with store id equal to the default store id.
protected function initCategories()
{
if (empty($this->categories)) {
$collection = $this->categoryColFactory->create();
$collection->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url_path');
/* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
foreach ($collection as $category) {
$structure = explode(self::DELIMITER_CATEGORY, $category->getPath());
$pathSize = count($structure);
$this->categoriesCache[$category->getId()] = $category;
if ($pathSize > 1) {
$path = [];
for ($i = 1; $i < $pathSize; $i++) {
$path[] = $collection->getItemById((int)$structure[$i])->getName();
}
$index = implode(self::DELIMITER_CATEGORY, $path);
$this->categories[$index] = $category->getId();
}
}
}
return $this;
}
In my opinion the collection should have a setStoreId(0) because the values for categories in the import csv should be the admin values.
$collection->setStoreId(0)
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url_path');
Copied from original issue: magento/magento2#6992