magento-engcom / import-export-improvements

Open Software License 3.0
31 stars 29 forks source link

CatalogImportExport categoryProcessor is using default store id values #88

Closed piotrekkaminski closed 6 years ago

piotrekkaminski commented 6 years ago

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

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

    Expected result

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

    Actual result

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

piotrekkaminski commented 6 years ago

From @sevos1984 on October 18, 2016 15:4

Internal ticket created MAGETWO-59827, thanks for posting

piotrekkaminski commented 6 years ago

From @MattDelac on December 18, 2016 20:12

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

piotrekkaminski commented 6 years ago

From @Tchinkatchuk on December 19, 2016 8:30

hello,

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

Thanks.

piotrekkaminski commented 6 years ago

From @MattDelac on December 19, 2016 8:44

@koenner01

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

Thank you

piotrekkaminski commented 6 years ago

From @vherasymenko on May 11, 2017 13:38

"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

piotrekkaminski commented 6 years ago

From @arshadpkm on June 10, 2017 11:41

I will check this one

piotrekkaminski commented 6 years ago

From @arshadpkm on June 13, 2017 11:12

@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?

piotrekkaminski commented 6 years ago

From @okorshenko on June 13, 2017 16:54

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

piotrekkaminski commented 6 years ago

From @magento-team on August 1, 2017 1:9

Internal ticket to track issue progress: MAGETWO-69864

piotrekkaminski commented 6 years ago

From @magento-engcom-team on October 18, 2017 11:30

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

dmanners commented 6 years ago

Thank you @pogster your PR https://github.com/magento-engcom/import-export-improvements/pull/112 has covered this issue.