Magento2: Wrong parent category url_key in URL

Created on 12 Apr 2016  路  18Comments  路  Source: magento/magento2

Preconditions

  • Magento 2.1 and 2.2

Steps to reproduce

We have two frontend storeviews in Magento 2.0.4, a French one and a German one. But the parent category url_key in the URL rewrites is always the German one, for both, the german and the french storeview.

Expected result

Expected URL rewrite for a sub-category:
https://example.com/fr/french-parent-category/french-sub-category/

Expected URL rewrite for a product:
https://example.com/fr/french-parent-category/french-product/

Actual result

Actual URL rewrite for sub-category:
https://example.com/fr/german-parent-category/french-sub-category/

Actual URL rewrite for a product:
https://example.com/fr/german-parent-category/french-product/

Fixed in 2.4.x Clear Description Format is valid

Most helpful comment

Hi @veloraven could you please reference a particular version (2.1.6, 2.1.7, etc.)? Or at least a commit that fixes the issue?

All 18 comments

internal issue MAGETWO-53443

Issue is not reproducible on 2.0.4, 2.0.7 and 2.1 with following combinations:

  • 1 website (default), 2 store views (default & french),
  • 2 websites (default, french). with one store view each (default - default, french - french)

Please verify that parent category (german-parent-category) in your instance has url-key value overriden on store view level. Please provide more details on the category entities values on different store views.

Hi @rganin

Thank you for looking into this issue.

All german url-keys are defined on default level (store view: all). The french url-keys are defined in the french store view. (In other words, no german url-keys are defined and/or overriden in the german store view).

also, just to clarify, we are using this combination: 1 website (default), 2 store views (german (sort order 0) & french (sort order 1)).

@bh-ref thanks for clarification, I will resume the investigation.

@bh-ref still cannot reproduce the issue on 2.0.4. Sorry for the silly question but do you have url-key for french store view on parent category? Or do you have some url-related extensions or customizations?

We have a similar problem.

Category configuration:
Default config > url_key = "products"
French store > url_key = "produits"

Product configuration:
Default config > url_key: "awesome_product"
French store > url_key: "un_produit"

The url rewrite will be created after saving the product, but the category path is not store specific.
French store rewrite: products/un_produit

We are using v2.0.6 and have traced down the problem.

Inside app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php:80 the product categories are fetched, but not store specific. Later on it calls generateForSpecificStoreView() and passes the storeId, but the storeId is never used for calculating the category url path inside the following method:

getUrlPathWithSuffix (app/code/Magento/CatalogUrlRewrite/Model/ProductUrlPathGenerator.php:94)

    public function getUrlPathWithSuffix($product, $storeId, $category = null)
    {
        return $this->getUrlPath($product, $category) . $this->getProductUrlSuffix($storeId);
    }

Hope this helps for this case.

Could reproduce the issue.
@stevenvdp great thanks for your input, proceeding with fix.

@stevenvdp thank you for diving into it!

@rganin we actually do have some extensions related to url-keys, but they should not interact in that way. But I am happy to test the eventual fix against our extension.

We also run into a very similar problem, which happens after trying to create url rewrites in an automated category/product import job.

What seems to fix it, is these two changes (based on the Magento 2.1.2 codebase):

  1. MagentoCatalogUrlRewriteModelProductUrlRewriteGenerator::generate
diff --git a/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php
index 6539a95..92ae0ec 100644
--- a/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php
+++ b/app/code/Magento/CatalogUrlRewrite/Model/ProductUrlRewriteGenerator.php
@@ -108,7 +108,8 @@ class ProductUrlRewriteGenerator

         $productCategories = $product->getCategoryCollection()
             ->addAttributeToSelect('url_key')
-            ->addAttributeToSelect('url_path');
+            ->addAttributeToSelect('url_path')
+            ->setStoreId($storeId);

         $urls = $this->isGlobalScope($storeId)
             ? $this->generateForGlobalScope($productCategories)
  1. MagentoCatalogUrlRewriteModelProductAnchorUrlRewriteGenerator::generate
diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Product/AnchorUrlRewriteGenerator.php b/app/code/Magento/CatalogUrlRewrite/Model/Product/AnchorUrlRewriteGenerator.php
index 972b5c0..8845c7a 100644
--- a/app/code/Magento/CatalogUrlRewrite/Model/Product/AnchorUrlRewriteGenerator.php
+++ b/app/code/Magento/CatalogUrlRewrite/Model/Product/AnchorUrlRewriteGenerator.php
@@ -55,7 +55,7 @@ class AnchorUrlRewriteGenerator
             $anchorCategoryIds = $category->getAnchorsAbove();
             if ($anchorCategoryIds) {
                 foreach ($anchorCategoryIds as $anchorCategoryId) {
-                    $anchorCategory = $this->categoryRepository->get($anchorCategoryId);
+                    $anchorCategory = $this->categoryRepository->get($anchorCategoryId, $storeId);
                     $urls[] = $this->urlRewriteFactory->create()
                         ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
                         ->setEntityId($product->getId())

This issue was fixed for ver. 2.1.
Closed.

Hi @veloraven could you please reference a particular version (2.1.6, 2.1.7, etc.)? Or at least a commit that fixes the issue?

Hi @veloraven you said that this was fixed for 2.1, but I still have this issue on 2.1.10 and 2.2. So where this is fixed?

This is still not fixed for the AnchorUrlRewriteGenerator.

This is the patch I'm using in the meantime;

diff --git a/Model/Product/AnchorUrlRewriteGenerator.php b/Model/Product/AnchorUrlRewriteGenerator.php
index a7cc894..c382d5f 100644
--- a/Model/Product/AnchorUrlRewriteGenerator.php
+++ b/Model/Product/AnchorUrlRewriteGenerator.php
@@ -61,7 +61,7 @@ class AnchorUrlRewriteGenerator
             $anchorCategoryIds = $category->getAnchorsAbove();
             if ($anchorCategoryIds) {
                 foreach ($anchorCategoryIds as $anchorCategoryId) {
-                    $anchorCategory = $this->categoryRepository->get($anchorCategoryId);
+                    $anchorCategory = $this->categoryRepository->get($anchorCategoryId, $storeId);
                     $urls[] = $this->urlRewriteFactory->create()
                         ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
                         ->setEntityId($product->getId())
-- 
2.14.1

Hm it seems that @hostep already applied this fix but later on it was reverted again.

@peterjaap: it was never accepted, I purposely closed the PR since it caused too many negative side effects, I can't remember which ones though...

But there is a new pending PR which might get merged someday: https://github.com/magento/magento2/pull/14344

This issue is already fixed in Magento 2.2.5 and 2.3-dev version.
default

Was this page helpful?
0 / 5 - 0 ratings