CatalogImportExport categoryProcessor is using default store id values
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');
Internal ticket created MAGETWO-59827, thanks for posting
Hello everyone,
As @koenner01, I have the same issue (Magento 2.1.2) and I don't know why categories do not work as attributes. Indeed, it's very important to synchronize by 'Admin value' and to easily change the name in each store view.
Can someone tell me if this issue is resolved with the new update (Magento 2.1.3) ? I did not see in the changelog. If not, when should it be resolved (approximately) ?
Thank you
hello,
I dot agree with @MattDelac & @koenner01. We should be able to do this. This is minimum valuable for export/import.
Thanks.
@koenner01
Did you find a hack to do it in another way ?
Thank you
"Steps:
- Install Magento latest develop version
- Go to Admin side
- Click ""Catalog > Categories""
- Add new category with name for example (test)
- Change location on top side on ""Default Store View""
- And change name of created cateogry on something else
- Create csv file for import with product (cateogry for product set ""Default Category/test"")
- Go to ""System > Import""
- Entity type choose ""Products""
- Import Behavior choose ""Add\Update""
- Choose created csv file
- Click Import
AR: 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 - 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"" ER: The product should be added to existing category 'test' successfully"
I will check this one
@okorshenko Need one help Travis fails with test INTEGRATION_INDEX=3, here is the link https://travis-ci.org/magento/magento2/jobs/242316296 But I am not sure how my commit related to this file. and how can I fix this?
@arshadpkm please, create pull request against 2.1-develop branch, but not 2.1
Internal ticket to track issue progress: MAGETWO-69864
@koenner01, thank you for your report. We've created internal ticket(s) MAGETWO-59827 to track progress on the issue.
This issue was moved to magento-engcom/import-export-improvements#88