Skip to content

Cannot assign products to categories not under tree root #8970

Closed

Description

@marius-bica

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: #7916)

Activity

  1. tdgroot commented on Jun 7, 2017

    @tdgroot
    Member

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

  2. 18 remaining items

  3. sabl0r commented on Sep 11, 2018

    @sabl0r

    Still happens to me with magento 2.2.5

  4. salehawal commented on Oct 17, 2018

    @salehawal

    same issue here @ 2.2.4

  5. tukker01 commented on Oct 22, 2018

    @tukker01

    same issue here @ 2.2.6

  6. AlexanderHuyghebaert commented on Nov 29, 2018

    @AlexanderHuyghebaert

    Issue still persisting 2.2.4

  7. digvijay2017 commented on Jan 21, 2019

    @digvijay2017

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

  8. sdzhepa commented on Jan 23, 2019

    @sdzhepa
    Contributor

    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 GITHUB-8970: Cannot assign products to categories not under tree root. #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 GITHUB-8970: Cannot assign products to categories not under tree root. #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 Cannot assign products to categories not under tree root #8970 (comment)

    Current Situation

    1. I have verified issue by Manual testing scenarios provided in the description for GITHUB-8970: Cannot assign products to categories not under tree root. #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:
      -- Cannot assign products to categories not under tree root #8970 (comment) by @inkobject
      -- Cannot assign products to categories not under tree root #8970 (comment) 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.

  9. Mrinza commented on Aug 19, 2019

    @Mrinza

    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?

  10. inkobject commented on Feb 21, 2020

    @inkobject

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

  11. rtshshukla commented on Jan 13, 2021

    @rtshshukla

    Hi
    I'm using the 2.3.5-p2 commerce cloud version, I'm facing the same issue. is there any patch or solution for this?
    Thanks in adavnce

  12. JMLucas96 commented on Apr 15, 2021

    @JMLucas96

    Same issue on Magento2.3.6 CE...

  13. ClintTec commented on Feb 23, 2022

    @ClintTec

    still happening on Magento 2.4.1p

  14. added a commit that references this issue on Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Component: CatalogFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releasebug report

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions