-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Moving category in hierarchy causes url_path to be incorrect when using different url keys in multiple storeviews #16202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is just a proof of concept (it's really dirty code I think), but applying this patch resolves the issues with the Expected:
Actual:
Patch:diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php
index 17d12ba563e..e93628aafa3 100644
--- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php
+++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Move.php
@@ -8,6 +8,7 @@ namespace Magento\CatalogUrlRewrite\Model\Category\Plugin\Category;
use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
+use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
class Move
{
@@ -21,16 +22,23 @@ class Move
*/
private $childrenCategoriesProvider;
+ /**
+ * @var StoreViewService
+ */
+ private $storeViewService;
+
/**
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
* @param ChildrenCategoriesProvider $childrenCategoriesProvider
*/
public function __construct(
CategoryUrlPathGenerator $categoryUrlPathGenerator,
- ChildrenCategoriesProvider $childrenCategoriesProvider
+ ChildrenCategoriesProvider $childrenCategoriesProvider,
+ StoreViewService $storeViewService
) {
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
+ $this->storeViewService = $storeViewService;
}
/**
@@ -53,6 +61,24 @@ class Move
) {
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
$category->getResource()->saveAttribute($category, 'url_path');
+
+ $clonedCategory = clone $category;
+
+ foreach ($clonedCategory->getStoreIds() as $storeId) {
+ if ($this->storeViewService->doesEntityHaveOverriddenUrlPathForStore(
+ $storeId,
+ $clonedCategory->getId(),
+ Category::ENTITY
+ )) {
+ $clonedCategory->setStoreId($storeId);
+ $clonedCategory->setUrlKey($clonedCategory->getResource()->getAttributeRawValue($clonedCategory->getId(), 'url_key', $storeId));
+
+ $clonedCategory->unsUrlPath();
+ $clonedCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($clonedCategory));
+ $clonedCategory->getResource()->saveAttribute($clonedCategory, 'url_path');
+ }
+ }
+
$this->updateUrlPathForChildren($category);
return $result;
@@ -68,6 +94,21 @@ class Move
$childCategory->unsUrlPath();
$childCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($childCategory));
$childCategory->getResource()->saveAttribute($childCategory, 'url_path');
+
+ foreach ($childCategory->getStoreIds() as $storeId) {
+ if ($this->storeViewService->doesEntityHaveOverriddenUrlPathForStore(
+ $storeId,
+ $childCategory->getId(),
+ Category::ENTITY
+ )) {
+ $childCategory->setStoreId($storeId);
+ $childCategory->setUrlKey($childCategory->getResource()->getAttributeRawValue($childCategory->getId(), 'url_key', $storeId));
+
+ $childCategory->unsUrlPath();
+ $childCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($childCategory));
+ $childCategory->getResource()->saveAttribute($childCategory, 'url_path');
+ }
+ }
}
}
} |
We've had the same problem on one of our multistores. We solved it with iazel/regenurl, the module contains two console commands that help you fix it. More info about the fix here in the readme. |
@Tjitse-E: quickly looking through the code, I can't see it fixing values in the |
@hostep, thank you for your report. |
@hostep you're right, the script doesn't fix the bad entities in the |
I just accidentally stumbled upon a commit in 2.3-develop which might fix this issue (not tested yet): 1f5d415 This also seems to be available already in 2.2-develop, but not included in any official release yet (at the of time of writing that is). |
Hi @hostep. Thank you for taking the time to work this issue! |
Is this still an issue in magento 2.4.x ? |
@nwtben: I wonder about that as well since I saw a few changes flying by in some pull request regarding the url rewrite generation with storeview specific data, so maybe it got fixed, but maybe also not ... Feel free to test and report back! 🙂 |
I can very easily break the frontend again. Here are some extra steps to perform after the steps from my opening post.
Expected
Actual:
DiscussionFor the developers who want to work on this issue, note that the bug starts happening between step 9 and 12, once you start dragging categories around, the In my opinion this should become a higher priority, shopowners using multiple storeviews and translated url_key's for categories still can't drag categories around in the hierarchy without breaking their url structure. Which is really bad for SEO. We have to tell our clients to delete a category, re-create it, re-add all children categories and re-assign all products, if they want to position it in another position, which is ridiculous. (/cc @sidolov) @engcom-Delta: can you re-open please? Also I have no powers to re-open my own tickets, so please don't suggest that I can, because we have very limited rights unfortunatly 🙁 |
@hostep thank you for the clarification! I have raised the priority of the picket to P1 since the flow to manage the categories you mentioned is awful. |
✅ Confirmed by @sdzhepa Issue Available: @sdzhepa, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself. |
I accidentally bumped into a commit which might fix this: MC-40371: Moving category in hierarchy causes url_path to be incorrect I'll try to test later when I find more energy. |
@magento I am working on this |
✔️ QA passed After fix: |
@dmitriyprime: which fix? MC-40371 or #32598 (MC-40780) ? If the latter, please comment on that pull request instead of here, thanks! 🙂 |
Hi @hostep, @victorpetryk. Thank you for your report and collaboration! The related internal Jira ticket The fix will be available with the upcoming |
For the people interested, MC-40371 fixed part of the problem, but it started breaking again after step 16. #32598 then solved the remainder, and now the bug no longer occurs as far as I can see. All category url_path's (and url rewrites having to do with these) are being generated correctly if you start dragging categories around in the hierarchy. If people are interested, here is a zip file with two patches that can be applied with https://github.com/cweagans/composer-patches or https://github.com/vaimo/composer-patches/ (use patch level 1 please): issue-16202-patches.zip These patches are tested on Magento 2.4.2 but could in theory also be applied to Magento 2.4.0 or 2.4.1. I haven't looked into backporting these all the way to 2.3.6 though (and maybe never will ...) Big thanks to @victorpetryk and @nikita-shcherbatykh and possibly some others involved here for finally fixing this huge bug that has existed since Magento 2 was released! 🥳 |
Uh oh!
There was an error while loading. Please reload this page.
Hi!
This is yet another issue in the seemingly endless catalog of Magento2 url bugs with categories and products when using multiple storeviews.
Preconditions
Steps to reproduce
catalog_category_entity_varchar
for the attributesurl_key
andurl_path
are correct. And all entries in theurl_rewrite
table are good.Feel free to review them at this point.
catalog_category_entity_varchar
for the attributeurl_path
are already incorrect on store view level, but the problem doesn't manifest itself yet on the frontend, since the url rewrites are still ok somehow. So let's start to break something on the frontend now.Expected result
Contents of the
catalog_category_entity_varchar
table for theurl_path
attribute (I took the liberty to "translate" the attribute_id and store_id to make the table easier to understand):When you look at the frontend of the shop and click on the following categories in the menu and look at the url, you'll see:
(exact same for French storeview)
Actual result
Contents of the
catalog_category_entity_varchar
table for theurl_path
attribute:When you look at the frontend of the shop and click on the following categories in the menu and look at the url, you'll see:
(exact same for French storeview)
Further discussion
I think this is because in the methods
afterChangeParent
andupdateUrlPathForChildren
of the classMagento\CatalogUrlRewrite\Model\Category\Plugin\Category\Move
should loop over all store id's and recalculate the url_path for every store view separately.Now, I don't think that fixing the code alone will be enough. You have to keep in mind that people have been running into this bug (and a lot of other url bugs) in the past 2 to 3 years. So it won't be enough to just fix the code.
I believe Magento should get some new command line commands for
bin/magento
which looks at all category's url_path attributes, and fix those if incorrect, and then also (maybe in a separate command) re-generate url rewrites (with an optional flag for creating 301 redirects, for shops already live vs shops in development).You could argue to write upgrade scripts which do this automatically when updating to a new version of Magento, but I don't think that would be a good idea, I think store owners need to execute these things manually, it might have SEO consequences if it happens automatically and store owners might not expect that to happen.
@akaplya: since you are responsible for the UrlRewrites components according to the wiki, I'm including you in here.
It might also be nice to get some kind of update from you regarding how it's going with fixing all those other store view related bugs with url rewrites for products and categories which still haven't been properly fixed (at least not in 2.2.4). If you don't know what I'm talking about, let me know, I can send you a whole list of issues.
Thanks!
The text was updated successfully, but these errors were encountered: