Skip to content

Conversation

engcom-Golf
Copy link
Contributor

Description (*)

The problem was in the process of URL rewrites generation if product is saved in global scope
This PR fixes issue by forcing generation URL rewrites for stores with scope overridden URL key in case if category for store was changed and category url_key is used for URL rewrite for products

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes URL Rewrite is lost when adding a existing product to another store view #30658

Manual testing scenarios (*)

  1. Create 2 websites with 2 stores
  2. Assign different root categories for each store and create sub category for each
  3. Create product
  4. Enable Stores->Configuration->Catalog->Catalog->Search Engine Optimization->Use Categories Path for Product URLs configuration
  5. Assign product to both websites by copying data from non default values (f.e. from base website store view) so url_key for new website should be non default
  6. Add remove categories for the product in new website.

Expected result

Url rewrite is generated for categor/product if category is assigned to product and removed if category is removed from product.

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)

@m2-assistant
Copy link

m2-assistant bot commented Feb 10, 2021

Hi @engcom-Golf. Thank you for your contribution
Here are some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

For more details, please, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@m2-community-project m2-community-project bot added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Feb 10, 2021
@engcom-Golf
Copy link
Contributor Author

@magento run all tests

foreach ($product->getStoreIds() as $id) {
if (!$this->isGlobalScope($id) &&
!$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore(
if ((!empty($changedStoreIds) && in_array((int)$id, $changedStoreIds))
Copy link
Contributor

@engcom-Kilo engcom-Kilo Feb 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it could be simplified to

Suggested change
if ((!empty($changedStoreIds) && in_array((int)$id, $changedStoreIds))
if (in_array((int)$id, $changedStoreIds)

@@ -276,4 +283,83 @@ private function isCategoryRewritesEnabled()
{
return (bool)$this->config->getValue('catalog/seo/generate_category_product_rewrites');
}

/**
* Return product changed categories store root category ids
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description is pretty confusing. Could please change to something like...

Suggested change
* Return product changed categories store root category ids
* Retrieve affected store ids for added/removed categories

Comment on lines 296 to 300
$actualCategoryIds = $product->getCategoryIds();
$result = [];
if ($oldCategoryIds === null) {
return $result;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As $product->getCategoryIds() may cost us extra request to Db it's better to end up earlier.
Also, why we skip url rewrite generation in case product didn't have categories but currently maybe does?

Suggested change
$actualCategoryIds = $product->getCategoryIds();
$result = [];
if ($oldCategoryIds === null) {
return $result;
}
if ($oldCategoryIds === null) {
return $result;
}
$actualCategoryIds = $product->getCategoryIds();
$result = [];

Comment on lines 319 to 329
$newCategories = array_filter(
$product->getCategoryCollection()->getItems(),
function (CategoryInterface $category) use ($product) {
return !in_array($category->getId(), $product->getOrigData('category_ids'));
}
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use here collection filter instead of post-processing?

Suggested change
$newCategories = array_filter(
$product->getCategoryCollection()->getItems(),
function (CategoryInterface $category) use ($product) {
return !in_array($category->getId(), $product->getOrigData('category_ids'));
}
);
$newCategories = $product->getCategoryCollection()->addIdFilter($product->getOrigData('category_ids'))->getItems();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not, as in that case, we can get either the wrong result if the collection was loaded before or break the original collection for further processing. Reload items here does not make sense.

}

/**
* Return added product categories store root category ids
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change comment to more clear one. See example above

}

/**
* Return store root category id parsed from path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this comment doesn't reflect actual method behavior

}

/**
* Return removed product categories store root category ids
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change comment to more clear one. See example above

Comment on lines 354 to 363
$storeIds = array_reduce(
$categories,
function (array $result, Category $category) {
$result[] = $category->getStoreIds();

return $result;
},
[]
);
return array_merge([], ...$storeIds);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably could be simplified to

Suggested change
$storeIds = array_reduce(
$categories,
function (array $result, Category $category) {
$result[] = $category->getStoreIds();
return $result;
},
[]
);
return array_merge([], ...$storeIds);
$result = [[]];
foreach ($categories as $category) {
$result[] = $category->getStoreIds();
}
$result = array_merge(...$result);
return array_unique($result);

* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testChangeCategoryGlobalScopeWithStoreOverriddenUrlKey()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to simplify this test using dataProvider?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it is a good idea as here we just have one more save and assertation, but if I explode this test to different variations it will work twice longer and wouldn't be simpler than this variant.

@engcom-Golf
Copy link
Contributor Author

@magento run all tests

@engcom-Golf
Copy link
Contributor Author

@magento run all tests

@engcom-Golf
Copy link
Contributor Author

@magento run all tests

@ishakhsuvarov
Copy link
Contributor

Closing this for now as it seems abandoned for a long time.

Please feel free to reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: CatalogUrlRewrite Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Release Line: 2.4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

URL Rewrite is lost when adding a existing product to another store view
4 participants