3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \CatalogUrlRewrite \Model ;
7
8
9
+ use Magento \Store \Model \Store ;
10
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Catalog \Model \Category ;
12
+ use Magento \Catalog \Model \Product ;
13
+ use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
14
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
15
+ use Magento \Store \Model \ScopeInterface ;
16
+ use Magento \Store \Model \StoreManagerInterface ;
17
+
18
+ /**
19
+ * Class ProductUrlPathGenerator
20
+ */
8
21
class ProductUrlPathGenerator
9
22
{
10
23
const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix ' ;
@@ -17,36 +30,36 @@ class ProductUrlPathGenerator
17
30
protected $ productUrlSuffix = [];
18
31
19
32
/**
20
- * @var \Magento\Store\Model\ StoreManagerInterface
33
+ * @var StoreManagerInterface
21
34
*/
22
35
protected $ storeManager ;
23
36
24
37
/**
25
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
38
+ * @var ScopeConfigInterface
26
39
*/
27
40
protected $ scopeConfig ;
28
41
29
42
/**
30
- * @var \Magento\CatalogUrlRewrite\Model\ CategoryUrlPathGenerator
43
+ * @var CategoryUrlPathGenerator
31
44
*/
32
45
protected $ categoryUrlPathGenerator ;
33
46
34
47
/**
35
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
48
+ * @var ProductRepositoryInterface
36
49
*/
37
50
protected $ productRepository ;
38
51
39
52
/**
40
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
41
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
53
+ * @param StoreManagerInterface $storeManager
54
+ * @param ScopeConfigInterface $scopeConfig
42
55
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
43
- * @param \Magento\Catalog\Api\ ProductRepositoryInterface $productRepository
56
+ * @param ProductRepositoryInterface $productRepository
44
57
*/
45
58
public function __construct (
46
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
47
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig ,
48
- \ Magento \ CatalogUrlRewrite \ Model \ CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
49
- \ Magento \ Catalog \ Api \ ProductRepositoryInterface $ productRepository
59
+ StoreManagerInterface $ storeManager ,
60
+ ScopeConfigInterface $ scopeConfig ,
61
+ CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
62
+ ProductRepositoryInterface $ productRepository
50
63
) {
51
64
$ this ->storeManager = $ storeManager ;
52
65
$ this ->scopeConfig = $ scopeConfig ;
@@ -57,8 +70,8 @@ public function __construct(
57
70
/**
58
71
* Retrieve Product Url path (with category if exists)
59
72
*
60
- * @param \Magento\Catalog\Model\ Product $product
61
- * @param \Magento\Catalog\Model\ Category $category
73
+ * @param Product $product
74
+ * @param Category $category
62
75
*
63
76
* @return string
64
77
*/
@@ -78,10 +91,10 @@ public function getUrlPath($product, $category = null)
78
91
/**
79
92
* Prepare URL Key with stored product data (fallback for "Use Default Value" logic)
80
93
*
81
- * @param \Magento\Catalog\Model\ Product $product
94
+ * @param Product $product
82
95
* @return string
83
96
*/
84
- protected function prepareProductDefaultUrlKey (\ Magento \ Catalog \ Model \ Product $ product )
97
+ protected function prepareProductDefaultUrlKey (Product $ product )
85
98
{
86
99
$ storedProduct = $ this ->productRepository ->getById ($ product ->getId ());
87
100
$ storedUrlKey = $ storedProduct ->getUrlKey ();
@@ -91,9 +104,9 @@ protected function prepareProductDefaultUrlKey(\Magento\Catalog\Model\Product $p
91
104
/**
92
105
* Retrieve Product Url path with suffix
93
106
*
94
- * @param \Magento\Catalog\Model\ Product $product
107
+ * @param Product $product
95
108
* @param int $storeId
96
- * @param \Magento\Catalog\Model\ Category $category
109
+ * @param Category $category
97
110
* @return string
98
111
*/
99
112
public function getUrlPathWithSuffix ($ product , $ storeId , $ category = null )
@@ -104,8 +117,8 @@ public function getUrlPathWithSuffix($product, $storeId, $category = null)
104
117
/**
105
118
* Get canonical product url path
106
119
*
107
- * @param \Magento\Catalog\Model\ Product $product
108
- * @param \Magento\Catalog\Model\ Category|null $category
120
+ * @param Product $product
121
+ * @param Category|null $category
109
122
* @return string
110
123
*/
111
124
public function getCanonicalUrlPath ($ product , $ category = null )
@@ -117,7 +130,7 @@ public function getCanonicalUrlPath($product, $category = null)
117
130
/**
118
131
* Generate product url key based on url_key entered by merchant or product name
119
132
*
120
- * @param \Magento\Catalog\Model\ Product $product
133
+ * @param Product $product
121
134
* @return string|null
122
135
*/
123
136
public function getUrlKey ($ product )
@@ -129,13 +142,15 @@ public function getUrlKey($product)
129
142
/**
130
143
* Prepare url key for product
131
144
*
132
- * @param \Magento\Catalog\Model\ Product $product
145
+ * @param Product $product
133
146
* @return string
134
147
*/
135
- protected function prepareProductUrlKey (\ Magento \ Catalog \ Model \ Product $ product )
148
+ protected function prepareProductUrlKey (Product $ product )
136
149
{
137
- $ urlKey = $ product ->getUrlKey ();
138
- return $ product ->formatUrlKey ($ urlKey === '' || $ urlKey === null ? $ product ->getName () : $ urlKey );
150
+ $ urlKey = (string )$ product ->getUrlKey ();
151
+ $ urlKey = trim (strtolower ($ urlKey ));
152
+
153
+ return $ urlKey ?: $ product ->formatUrlKey ($ product ->getName ());
139
154
}
140
155
141
156
/**
@@ -153,7 +168,7 @@ protected function getProductUrlSuffix($storeId = null)
153
168
if (!isset ($ this ->productUrlSuffix [$ storeId ])) {
154
169
$ this ->productUrlSuffix [$ storeId ] = $ this ->scopeConfig ->getValue (
155
170
self ::XML_PATH_PRODUCT_URL_SUFFIX ,
156
- \ Magento \ Store \ Model \ ScopeInterface::SCOPE_STORE ,
171
+ ScopeInterface::SCOPE_STORE ,
157
172
$ storeId
158
173
);
159
174
}
0 commit comments