Skip to content
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
@piotrekkaminski

Description

@piotrekkaminski

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

  1. Magento 2.1
  2. PHP7.0

Steps to reproduce

  1. Create a category 'Test'
  2. Set the category name to something completely different on default storeview
  3. Create a csv with catalog product data
  4. Set 'Default Category/Test' as the 'categories' value
  5. Import

Expected result

  1. The product is added to the existing category 'Test' (which we just created)

Actual result

  1. The product will not be added to the existing category
  2. A new category with the exact same path is created and the product is put in the new category
    OR
  3. 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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions