Description
Preconditions
- Problem is evident on 2.1.9
- Magento install with two separate stores (Store A and Store B), with two separate catalog root categories (
Store A Root
andStore B Root
) - At least one top-level category under each root category (
Store A Root -> Category 1
,Store B Root -> Category 2
) - Store is configured not to include categories in product URL paths (uncertain whether this matters)
Steps to reproduce
- Create and save a product (e.g., SKU
has-special-key
) with the URL key "url-test-one", placing it in the categoriesStore A Root -> Category 1
andStore B Root -> Category 2
. - Edit
has-special-key
and switch to the Store B view scope. Change the URL key to "url-test-one-override" and save. - The table
url_rewrite
now has at least two records forhas-special-key
:- Request path "url-test-one.html" for the Store A view
- Request path "url-test-one-override.html" for the Store B view
- Create and save a product (e.g., SKU
no-special-key
) with the URL key "url-test-two", placing it in the categoriesStore A Root -> Category 1
andStore B Root -> Category 2
. - The table
url_rewrite
now also has at least two records forno-special-key
:- Request path "url-test-two.html" for the Store A view
- Request path "url-test-two.html" for the Store B view
- Edit the category
Store B Root -> Category 2
. Change the URL key in order to trigger regeneration of products belonging to the category, and save.
Expected result
The url_rewrite
table should still have the following records:
- Two for
has-special-key
:- Request path "url-test-one.html" for the Store A view
- Request path "url-test-one-override.html" for the Store B view
- Two for
no-special-key
:- Request path "url-test-two.html" for the Store A view
- Request path "url-test-two.html" for the Store B view
Actual result
The url_rewrite
table is missing the Store B record for has-special-key
(the one that should have the request path "url-test-one-override.html").
Details
This happens because of a combination of two factors:
Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::generateForGlobalScope
contains a check for$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore
and does not proceed to generate rewrite paths for any store view where the URL key is different than the global scope. (Presumably because, the product model having been loaded at the default scope, it does not contain the appropriate values to generate rewrites for the given store.)Magento\UrlRewrite\Model\Storage\DbStorage::createFilterDataBasedOnUrls
uses the generated list of URL rewrites to be created to create an array of filters to use in deleting existing rewrite records. The filters simply specify entity IDs and store IDs.
In the reproduction scenario outlined above: the check in generateForGlobalScope
means no new URL rewrite records will be created for has-special-key
in Store B, because it has an overriding URL key for that store. If that were the only product in the category being saved, we wouldn't have a problem. However, no-special-key
does not have an overriding URL key on Store B, so it does have new URL rewrite records for that store. And so createFilterDataBasedOnUrls
ends up returning a filter array that will essentially say "delete all product rewrite records for SKUs has-special-key
and no-special-key
and for Store A and Store B. has-special-key
's rewrite record for Store B gets deleted, but no new record takes its place.
Possible solutions
Two possible solutions are immediately apparent but have their own problems:
- Modify
Magento\UrlRewrite\Model\Storage\DbStorage::createFilterDataBasedOnUrls
so that the format of the filter is more specific about combinations of products and stores that should be deleted. But this would require re-factoring all code that uses the results of this method. - Modify
Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::generateForGlobalScope
to avoid the check for an overriding URL key and allowgenerateForSpecificStoreView
to run regardless. Also modifygenerateForSpecificStoreView
to load up a new instance of the product in the right store scope, if the product's store scope doesn't match the passed store ID and if an overriding URL key exists. But if there are many such products with overriding URL keys on specific stores, this solution could result in unacceptable performance as many individual product loads are done.