magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

CatalogImportExport categoryProcessor is using default store id values

Open koenner01 opened this issue 10 years ago • 11 comments

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');

koenner01 avatar Oct 13 '16 13:10 koenner01

Internal ticket created MAGETWO-59827, thanks for posting

sevos1984 avatar Oct 18 '16 15:10 sevos1984

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

MattDelac avatar Dec 18 '16 20:12 MattDelac

hello,

I dot agree with @MattDelac & @koenner01. We should be able to do this. This is minimum valuable for export/import.

Thanks.

Tchinkatchuk avatar Dec 19 '16 08:12 Tchinkatchuk

@koenner01

Did you find a hack to do it in another way ?

Thank you

MattDelac avatar Dec 19 '16 08:12 MattDelac

"Steps:

  1. Install Magento latest develop version
  2. Go to Admin side
  3. Click ""Catalog > Categories""
  4. Add new category with name for example (test)
  5. Change location on top side on ""Default Store View""
  6. And change name of created cateogry on something else
  7. Create csv file for import with product (cateogry for product set ""Default Category/test"")
  8. Go to ""System > Import""
  9. Entity type choose ""Products""
  10. Import Behavior choose ""Add\Update""
  11. Choose created csv file
  12. 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"

vherasymenko avatar May 11 '17 13:05 vherasymenko

I will check this one

arshadpkm avatar Jun 10 '17 11:06 arshadpkm

@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 avatar Jun 13 '17 11:06 arshadpkm

@arshadpkm please, create pull request against 2.1-develop branch, but not 2.1

okorshenko avatar Jun 13 '17 16:06 okorshenko

Internal ticket to track issue progress: MAGETWO-69864

magento-team avatar Aug 01 '17 01:08 magento-team

@koenner01, thank you for your report. We've created internal ticket(s) MAGETWO-59827 to track progress on the issue.

magento-engcom-team avatar Oct 18 '17 11:10 magento-engcom-team

This issue was moved to magento-engcom/import-export-improvements#88

piotrekkaminski avatar Jan 03 '18 16:01 piotrekkaminski