Description
Preconditions (*)
- Magento 2.3.5-p1 (Cloud Edition)
- Magento 2.4-develop
Steps to reproduce (*)
We have a module that will regenerate the URLs of products enmasse - this is useful for many reasons but we have discovered that on a site where Store Views use different URL keys (category) incorrect URLs can be generated. This occurs when product URLS contain category URLs.
The module uses the Core Magento urlRewriteGenerator to do this -
public function regenerateEntity($products, $storeId)
{
$this->getConnection()->beginTransaction();
$currentProductId = 0;
try {
$urls = [];
/** @var \Magento\Catalog\Model\Product $product */
foreach ($products as $product) {
$currentProductId = $product->getId();
$this->logger->info(\__('Regeneration of product %1', $product->getSku()));
$product->setStoreId($storeId);
if ($product->getVisibility() !== Visibility::VISIBILITY_NOT_VISIBLE) {
$urls = \array_merge($urls, $this->urlRewriteGenerator->generate($product));
}
}
if (!empty($urls)) {
$this->urlPersist->replace($urls);
}
$this->getConnection()->commit();
} catch (\Throwable $e) {
$this->getConnection()->rollback();
$message = \__(
'Error occurred for product id %1, store id %2: %3',
$currentProductId,
$storeId,
$e->getMessage()
);
$this->logger->error($message);
}
}
We have also found that the same bugs below cause URLS to be created incorrectly for a product when an Admin user switches Visibility Status.
- Ensure Category URLs in Product URLS is active
- Create 3 level category - Men > Collections > Premium
- Add product to this category set visibility to "Not Visible"
- Create a new Store View - Italian.
- Edit Categories from Admin and switch to Italian Store View
- Change url_key on Men to uomo
- Change url_key on Collections to collezioni
- Change product from "Not Visible" to "Catalog, Search"
Expected result (*)
- Find the record for this product on this category in url_rewrite.
- Record should show for request path : uomo/collezioni/premium/
Actual result (*)
- Record shows : men/collections/premium/
The Regeneration script causes the same issue. Also the script can cause TWO URLs to appear for a product - one in the StoreView language and one in the Default View language - both work but Magento uses the Default View one.
BUGS FOUND -
ProductScopeRewriteGenerator::getCategoryWithOverriddenUrlKey - The problem is that the method checks to see if the category has an overridden URL key for that store view. If not then it uses the Default Category. The flaw here is that the category in question could be several layers down and whilst the URL key might still be the default, the higher level ones might not.
EG as above on a 3rd Level category :
men/collections/premium/
In Italian this is :
uomo/collezioni/premium/
So Magento sees the key 'premium' as the same as default, uses default level and thus the Italian site has the English URLS
SOLUTION - The method should always return the store view level category.
AnchorUrlRewriteGenerator::generate
This method handles rewrites for anchor categories. However, where it gets the category (line 71) it always gets the default level. This means the URL created and inserted is the Default Store one.
Result is that a product can have TWO URLs - one in the language of the Store View and one in the language of the default store. Both work, but Magento seems to want to use the Default Store one on the front-end
SOLUTION - This line should pass the $storeId.
Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.
- Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status