8
8
use Magento \CatalogUrlRewrite \Model \ProductUrlRewriteGenerator ;
9
9
use Magento \Store \Model \Store ;
10
10
use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Store \Model \ScopeInterface ;
12
+ use Magento \Catalog \Helper \Product as HelperProduct ;
11
13
12
14
/**
13
15
* Sitemap resource product collection model
@@ -88,6 +90,13 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
88
90
* @var \Magento\Catalog\Helper\Image
89
91
*/
90
92
private $ catalogImageHelper ;
93
+
94
+ /**
95
+ * Scope Config
96
+ *
97
+ * @var \Magento\Framework\App\Config\ScopeConfigInterface
98
+ */
99
+ private $ scopeConfig ;
91
100
92
101
/**
93
102
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
@@ -102,6 +111,7 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
102
111
* @param string $connectionName
103
112
* @param \Magento\Catalog\Model\Product $productModel
104
113
* @param \Magento\Catalog\Helper\Image $catalogImageHelper
114
+ * @param \Magento\Framework\App\Config\ScopeConfigInterface|null $scopeConfig
105
115
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
106
116
*/
107
117
public function __construct (
@@ -116,7 +126,8 @@ public function __construct(
116
126
\Magento \Catalog \Model \Product \Media \Config $ mediaConfig ,
117
127
$ connectionName = null ,
118
128
\Magento \Catalog \Model \Product $ productModel = null ,
119
- \Magento \Catalog \Helper \Image $ catalogImageHelper = null
129
+ \Magento \Catalog \Helper \Image $ catalogImageHelper = null ,
130
+ \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig = null
120
131
) {
121
132
$ this ->_productResource = $ productResource ;
122
133
$ this ->_storeManager = $ storeManager ;
@@ -129,6 +140,8 @@ public function __construct(
129
140
$ this ->productModel = $ productModel ?: ObjectManager::getInstance ()->get (\Magento \Catalog \Model \Product::class);
130
141
$ this ->catalogImageHelper = $ catalogImageHelper ?: ObjectManager::getInstance ()
131
142
->get (\Magento \Catalog \Helper \Image::class);
143
+ $ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()
144
+ ->get (\Magento \Framework \App \Config \ScopeConfigInterface::class);
132
145
parent ::__construct ($ context , $ connectionName );
133
146
}
134
147
@@ -272,6 +285,10 @@ public function getCollection($storeId)
272
285
}
273
286
274
287
$ connection = $ this ->getConnection ();
288
+ $ urlsConfigCondition = '' ;
289
+ if ($ this ->isCategoryProductURLsConfig ($ storeId )) {
290
+ $ urlsConfigCondition = 'NOT ' ;
291
+ }
275
292
276
293
$ this ->_select = $ connection ->select ()->from (
277
294
['e ' => $ this ->getMainTable ()],
@@ -282,7 +299,8 @@ public function getCollection($storeId)
282
299
[]
283
300
)->joinLeft (
284
301
['url_rewrite ' => $ this ->getTable ('url_rewrite ' )],
285
- 'e.entity_id = url_rewrite.entity_id AND url_rewrite.is_autogenerated = 1 AND url_rewrite.metadata IS NULL '
302
+ 'e.entity_id = url_rewrite.entity_id AND url_rewrite.is_autogenerated = 1 AND url_rewrite.metadata IS '
303
+ . $ urlsConfigCondition . 'NULL '
286
304
. $ connection ->quoteInto (' AND url_rewrite.store_id = ? ' , $ store ->getId ())
287
305
. $ connection ->quoteInto (' AND url_rewrite.entity_type = ? ' , ProductUrlRewriteGenerator::ENTITY_TYPE ),
288
306
['url ' => 'request_path ' ]
@@ -449,4 +467,20 @@ private function getProductImageUrl($image)
449
467
450
468
return $ imgUrl ;
451
469
}
470
+
471
+ /**
472
+ * Return Use Categories Path for Product URLs config value
473
+ *
474
+ * @param $storeId
475
+ *
476
+ * @return bool
477
+ */
478
+ private function isCategoryProductURLsConfig ($ storeId )
479
+ {
480
+ return (bool )$ this ->scopeConfig ->getValue (
481
+ HelperProduct::XML_PATH_PRODUCT_URL_USE_CATEGORY ,
482
+ ScopeInterface::SCOPE_STORE ,
483
+ $ storeId
484
+ );
485
+ }
452
486
}
0 commit comments