Skip to content

[Issue] Prevent side effect on category objects store_id and url_key on save #29585

Closed
@m2-assistant

Description

@m2-assistant

This issue is automatically created based on existing pull request: #28164: Prevent side effect on category objects store_id and url_key on save


Preconditions (*)

Magento 2.4-develop

Category URL rewrites are saved by an "after save" observer for categories. But if saved for global scope, the URL rewrite generator itself modifies the passed category object for each assigned store to generate URL rewrites for all stores. Afterwards, url_key, url_path and store_id have the values of the last processed store id. This has side effects on later observers or other code that works on the category object after saving it.

Since the default store (0) is always the first item of $category->getStoreIds() I chose to clone the category object only for the other stores, so that url_key and url_path for the global scope are still set to the original object.

Related Pull Requests

Fixed Issues (if relevant)

  1. magento/magento2#<issue_number>: Issue title

Steps to reproduce (*)

Abbreviated test case

$category->setStoreId(0);
$category->setUrlKey('url-key-for-global-scope');
$categoryResource->save($category);

Expected result (*)

$category->getStoreId() should return 0

Actual result (*)

$category->getStoreId() returns value of the last processed store id.
image

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: CatalogUrlRewriteFixed in 2.4.xThe issue has been fixed in 2.4-develop branchIssue: 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 developmentPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: doneReported on 2.4.0Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchSeverity: S3Affects non-critical data or functionality and does not force users to employ a workaround.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions