@@ -71,16 +71,33 @@ public function execute(\Magento\Framework\Event\Observer $observer)
71
71
$ useDefaultAttribute = !$ category ->isObjectNew () && !empty ($ category ->getData ('use_default ' )['url_key ' ]);
72
72
if ($ category ->getUrlKey () !== false && !$ useDefaultAttribute ) {
73
73
$ resultUrlKey = $ this ->categoryUrlPathGenerator ->getUrlKey ($ category );
74
- if (empty ($ resultUrlKey )) {
75
- throw new \Magento \Framework \Exception \LocalizedException (__ ('Invalid URL key ' ));
76
- }
77
- $ category ->setUrlKey ($ resultUrlKey )
78
- ->setUrlPath ($ this ->categoryUrlPathGenerator ->getUrlPath ($ category ));
79
- if (!$ category ->isObjectNew ()) {
80
- $ category ->getResource ()->saveAttribute ($ category , 'url_path ' );
81
- if ($ category ->dataHasChangedFor ('url_path ' )) {
82
- $ this ->updateUrlPathForChildren ($ category );
83
- }
74
+ $ this ->updateUrlKey ($ category , $ resultUrlKey );
75
+ } else if ($ useDefaultAttribute ) {
76
+ $ resultUrlKey = $ category ->formatUrlKey ($ category ->getOrigData ('name ' ));
77
+ $ this ->updateUrlKey ($ category , $ resultUrlKey );
78
+ $ category ->setUrlKey (null )->setUrlPath (null );
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Update Url Key
84
+ *
85
+ * @param Category $category
86
+ * @param string $urlKey
87
+ * @throws \Magento\Framework\Exception\LocalizedException
88
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
89
+ */
90
+ private function updateUrlKey ($ category , $ urlKey )
91
+ {
92
+ if (empty ($ urlKey )) {
93
+ throw new \Magento \Framework \Exception \LocalizedException (__ ('Invalid URL key ' ));
94
+ }
95
+ $ category ->setUrlKey ($ urlKey )
96
+ ->setUrlPath ($ this ->categoryUrlPathGenerator ->getUrlPath ($ category ));
97
+ if (!$ category ->isObjectNew ()) {
98
+ $ category ->getResource ()->saveAttribute ($ category , 'url_path ' );
99
+ if ($ category ->dataHasChangedFor ('url_path ' )) {
100
+ $ this ->updateUrlPathForChildren ($ category );
84
101
}
85
102
}
86
103
}
@@ -110,8 +127,13 @@ protected function updateUrlPathForChildren(Category $category)
110
127
} else {
111
128
$ children = $ this ->childrenCategoriesProvider ->getChildren ($ category , true );
112
129
foreach ($ children as $ child ) {
130
+ /** @var Category $child */
113
131
$ child ->setStoreId ($ category ->getStoreId ());
114
- $ this ->updateUrlPathForCategory ($ child );
132
+ if ($ child ->getParentId () === $ category ->getId ()) {
133
+ $ this ->updateUrlPathForCategory ($ child , $ category );
134
+ } else {
135
+ $ this ->updateUrlPathForCategory ($ child );
136
+ }
115
137
}
116
138
}
117
139
}
@@ -131,12 +153,14 @@ protected function isGlobalScope($storeId)
131
153
* Update url path for category.
132
154
*
133
155
* @param Category $category
156
+ * @param Category|null $parentCategory
134
157
* @return void
158
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
135
159
*/
136
- protected function updateUrlPathForCategory (Category $ category )
160
+ protected function updateUrlPathForCategory (Category $ category, Category $ parentCategory = null )
137
161
{
138
162
$ category ->unsUrlPath ();
139
- $ category ->setUrlPath ($ this ->categoryUrlPathGenerator ->getUrlPath ($ category ));
163
+ $ category ->setUrlPath ($ this ->categoryUrlPathGenerator ->getUrlPath ($ category, $ parentCategory ));
140
164
$ category ->getResource ()->saveAttribute ($ category , 'url_path ' );
141
165
}
142
166
}
0 commit comments