Skip to content

Commit f8c70d5

Browse files
author
Stanislav Idolov
authored
ENGCOM-1351: [Forwardport] Fixing wrong sitemap product url #4788 #14786
2 parents 58dd8e2 + 5b54eff commit f8c70d5

File tree

1 file changed

+36
-2
lines changed
  • app/code/Magento/Sitemap/Model/ResourceModel/Catalog

1 file changed

+36
-2
lines changed

app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
99
use Magento\Store\Model\Store;
1010
use Magento\Framework\App\ObjectManager;
11+
use Magento\Store\Model\ScopeInterface;
12+
use Magento\Catalog\Helper\Product as HelperProduct;
1113

1214
/**
1315
* Sitemap resource product collection model
@@ -88,6 +90,13 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
8890
* @var \Magento\Catalog\Helper\Image
8991
*/
9092
private $catalogImageHelper;
93+
94+
/**
95+
* Scope Config
96+
*
97+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
98+
*/
99+
private $scopeConfig;
91100

92101
/**
93102
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
@@ -102,6 +111,7 @@ class Product extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
102111
* @param string $connectionName
103112
* @param \Magento\Catalog\Model\Product $productModel
104113
* @param \Magento\Catalog\Helper\Image $catalogImageHelper
114+
* @param \Magento\Framework\App\Config\ScopeConfigInterface|null $scopeConfig
105115
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
106116
*/
107117
public function __construct(
@@ -116,7 +126,8 @@ public function __construct(
116126
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
117127
$connectionName = null,
118128
\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
120131
) {
121132
$this->_productResource = $productResource;
122133
$this->_storeManager = $storeManager;
@@ -129,6 +140,8 @@ public function __construct(
129140
$this->productModel = $productModel ?: ObjectManager::getInstance()->get(\Magento\Catalog\Model\Product::class);
130141
$this->catalogImageHelper = $catalogImageHelper ?: ObjectManager::getInstance()
131142
->get(\Magento\Catalog\Helper\Image::class);
143+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()
144+
->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);
132145
parent::__construct($context, $connectionName);
133146
}
134147

@@ -272,6 +285,10 @@ public function getCollection($storeId)
272285
}
273286

274287
$connection = $this->getConnection();
288+
$urlsConfigCondition = '';
289+
if ($this->isCategoryProductURLsConfig($storeId)) {
290+
$urlsConfigCondition = 'NOT ';
291+
}
275292

276293
$this->_select = $connection->select()->from(
277294
['e' => $this->getMainTable()],
@@ -282,7 +299,8 @@ public function getCollection($storeId)
282299
[]
283300
)->joinLeft(
284301
['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'
286304
. $connection->quoteInto(' AND url_rewrite.store_id = ?', $store->getId())
287305
. $connection->quoteInto(' AND url_rewrite.entity_type = ?', ProductUrlRewriteGenerator::ENTITY_TYPE),
288306
['url' => 'request_path']
@@ -449,4 +467,20 @@ private function getProductImageUrl($image)
449467

450468
return $imgUrl;
451469
}
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+
}
452486
}

0 commit comments

Comments
 (0)