Magento2: Cannot assign products to categories not under tree root

Created on 22 Mar 2017  路  17Comments  路  Source: magento/magento2


Preconditions

Magento version: 2.1.4 community edition
PHP version 7.0.15

Steps to reproduce

Try to assign some products to categories programmatically using a CategoryLinkManagement object. The code looks something like this:

    $this
        ->categoryLinkManagement
        ->assignProductToCategories(
             $someValidProduct->getSku(),
             [$someValidCategory->getId()]
         );

where the categoryLinkManagement property is injected via DI into the current class.

Expected result

The product gets assigned to the category.

Actual result

I get an error telling me that "Could not save product "2048" with position 0 to category 42"

What I think

After digging around in the code, it seems that the issue stems from this piece of code in the Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator class, the isCategoryProperForGenerating method:

if ($category->getParentId() != \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
    list(, $rootCategoryId) = $category->getParentIds();
    return $rootCategoryId == $this->storeManager->getStore($storeId)->getRootCategoryId();
}

more specific,

    list(, $rootCategoryId) = $category->getParentIds();

From what I can tell, the code simply tries to get the first element from the array of category parent ids and assign it to $rootCategoryId, but it does that by relying on php 5 or less behavior. Here is what the php manual says about list:

Warning
In PHP 5, list() assigns the values starting with the right-most parameter. In PHP 7, list() starts with the left-most parameter.

They are even warning that list is unpredictable:

Generally speaking, it is advisable to avoid relying on a specific order of operation, as this may change again in the future.

So this code fails to get the root category in php 7 and it even generates a notice if the parent ids array has less than 2 elements, since there is no element with an index of 1.
Exception: Notice: Undefined offset: 1 in vendor/magento/module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php on line 195
(here is a related issue detailing this: https://github.com/magento/magento2/issues/7916)

Catalog Fixed in 2.3.x Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x bug report

Most helpful comment

same issue here @ 2.2.6

All 17 comments

Would be great to hear what the core team has to say about this, because it's kind of an edgy case..

The issue has been fixed and deliverd to 2.2-develop branch. Will be available with 2.2.2 release

Internal ticket to track issue progress: MAGETWO-82972

Hi @marius-bica. Thank you for your report.
The issue has been fixed in magento-engcom/magento2ce#1285 by @magento-engcom-team in 2.3-develop branch
Related commit(s):

  • ad6c0af7dddbe2855a336acd3fd55994851e6d26
  • 6d3d70a72f08cba36c3aef6b59eea8db40eb4071
  • 6b0c02c5fd00d7a4237c4e1fae40dc265e3e43c3
  • c8abb139f8006826989fe4231dabc21fd4a02fd0
  • c6a9b5f7b13554958b547f387637c4288eb60b0b
  • c867b81af936e01a3d8ba8baa7128de06a5b7cd5
  • 88a3ad00d3ca97da847531c86bf64055dc1d790b
  • d87e316a2d6ee55debe2e712359adba8cb70fdb2
  • c76b04c9acde117c1021270500c8ded6954f6f05
  • 5ae21099aaa19699525c798f9ac0ef10db47b311
  • 931133abf574f1db96450f11f5980ef359d32967
  • 4c927a0585f11921c5e422b7e4a15b787e6338bd
  • 060a8f9f2b08ed97b31667eb76cace1dc0e642be
  • 4da2a7d2657b0da67f8be43bffc85f04b5b7144e
  • 008ef5583892d73c04628f003a2ce3d97fe0ed72
  • d3642e88d26a9240a1a4d648d37d984a785672a9
  • d37ce089d56c230f5896d5d7e922bd928b3726e6
  • b22b232563359f9cbcbf09709e63a87be7e81446
  • 6c02a2d3a272e872d549881d91ed6d5235a33f77
  • f2ec2ea175469636acec0bf9f65f0601854f89a2

The fix will be available with the upcoming patch release.

I am getting this after updating to 2.2.4

Perhaps a similar error in 2.2.3
Specifically trying to removing prodcut from a parent category but leaving it in sub category:
Current state:
Parent Cat
-Sub Cat
product here

Desired state remove Parent Cat:
-Sub Cat
product here

Result: Could not save product "205" with position 0 to category 3

I can move the product freely in subcategories. I just cant remove it from from the root.

I am getting this on Magento 2.2.3
I was first clueless but I narrowed down it a bit. here is the scenario.
Cat 174 has multiple products, then it was disabled. Now from the product edit page, i tried to remove the product from this category Klm. I receive error
Could not save product "3572" with position 0 to category 174
I repeat the steps and next time it says You saved the product. and works as expected.

  • Root Catalog (id=1)
    -- Main Cat (id=2)
    --- Abc
    ---- Hij
    ---- Klm (id=27, status=disabled)
    ---- Xyz

Best,

Same issue on version 2.2.5

magento 2.2.4

Could not save product with position to category
Unique constraint violation found

it accrues after trying to remove categories from a product

Still happens to me with magento 2.2.5

same issue here @ 2.2.4

same issue here @ 2.2.6

Issue still persisting 2.2.4

How can close this issue, even this issue already existing in magento 2.2.6.

Hello @graffxdesign @inkobject @harshvardhanmalpani @koopjesboom @salehawal @tukker01 @AlexanderHuyghebaert @digvijay2017

First of all, thank you all for the feedback, comments and collaboration!

We have investigated this issue and defined:

The issue and the fix details

  1. The initially described issue was fixed with PR #11817. Changes were applied on _"public function isCategoryProperForGenerating"_, see details by This Link
  2. The issue was fixed based on proposed solution by reporter in the description
  3. The issue was tested and confirmed based on the description for #11817
  4. All code changes were delivered with 2.2.2
    -- It was not in 2.2.1: Link to Code
    -- The fix appears starting from 2.2.2: Link to Code
  5. Additional details about commits can be found here https://github.com/magento/magento2/issues/8970#issuecomment-361430191

Current Situation

  1. I have verified issue by Manual testing scenarios provided in the description for #11817. And can confirm:
    -- the issue was reproducible in 2.2.1
    -- has been fixed in 2.2.2 - cannot be reproduced by testing scenarios
    -- 2.3.0 - cannot be reproduced by testing scenarios, fixed
    -- 2.2-develop - cannot be reproduced by testing scenarios, fixed
    -- 2.3-develop - cannot be reproduced by testing scenarios, fixed
  2. Also, I tried to replicate error using steps from comments:
    -- https://github.com/magento/magento2/issues/8970#issuecomment-389771781 by @inkobject
    -- https://github.com/magento/magento2/issues/8970#issuecomment-393159112 by @harshvardhanmalpani

And as result:

  • I cannot reproduce it by provided steps. But based on comments amount, I think some important preconditions or step(s) were missed. Like install type, PHP version, Magento mode, order of provided steps or other very important conditions/steps(mass actions, import/export, SysConf settings, Category settings, etc)
  • I cannot confirm that both issues have the same root cause. It could be similar symptoms related to a different part(s) of code.

Solution

Based on all of mentioned above:
The best solution it's to create a new issue. With detailed information about how it can be reproduced on the clear/vanilla Magento versions(2.2-develop and 2.3-develop)

To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me $VERSION instance

where $VERSION is version tags (starting from 2.2.0+) or develop branches (for example: 2.3-develop).
For more details, please, review the Magento Contributor Assistant documentation.

Unfortunately, I am not able to reproduce error that was mentioned in the comments and because of that cannot create a new issue by myself.
And that is why we need you for help with it.

I suppose a proposed solution could make you sad.
But it is the most efficient way to resolve it as quickly as possible rather than add comments to closed, more than a year ago, issue.

The changes are already in 2.2.4 but i receive same error when unassigned any product from root category. How i will solve this?

@sdzhepa to continue the "respond to comments one year later" trend, thank you for your input. If it is of any use, I have since moved to different hosts and have made many changes to the environment. I have not seen the issue crop up since. I agree with your observation regarding my experience.

Was this page helpful?
0 / 5 - 0 ratings