diff --git a/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php b/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php index 60a5ba512a49e..6e11833e5bc4d 100644 --- a/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php +++ b/app/code/Magento/Sitemap/Model/ResourceModel/Catalog/Product.php @@ -189,9 +189,10 @@ protected function _addFilter($storeId, $attributeCode, $value, $type = '=') * * @param int $storeId * @param string $attributeCode + * @param string $column Add attribute value to given column * @return void */ - protected function _joinAttribute($storeId, $attributeCode) + protected function _joinAttribute($storeId, $attributeCode, $column = null) { $connection = $this->getConnection(); $attribute = $this->_getAttribute($attributeCode); @@ -204,6 +205,8 @@ protected function _joinAttribute($storeId, $attributeCode) . ' AND ' . $connection->quoteInto($attrTableAlias . '.attribute_id = ?', $attribute['attribute_id']), [] ); + // Global scope attribute value + $columnValue = 't1_' . $attributeCode . '.value'; if (!$attribute['is_global']) { $attrTableAlias2 = 't2_' . $attributeCode; @@ -214,6 +217,15 @@ protected function _joinAttribute($storeId, $attributeCode) . ' AND ' . $connection->quoteInto($attrTableAlias2 . '.store_id = ?', $storeId), [] ); + // Store scope attribute value + $columnValue = $this->getConnection()->getIfNullSql('t2_' . $attributeCode . '.value', $columnValue); + } + + // Add attribute value to result set if needed + if (isset($column)) { + $this->_select->columns([ + $column => $columnValue + ]); } } @@ -282,30 +294,15 @@ public function getCollection($storeId) // Join product images required attributes $imageIncludePolicy = $this->_sitemapData->getProductImageIncludePolicy($store->getId()); if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_NONE != $imageIncludePolicy) { - $this->_joinAttribute($store->getId(), 'name'); - $this->_select->columns( - ['name' => $this->getConnection()->getIfNullSql('t2_name.value', 't1_name.value')] - ); - + $this->_joinAttribute($store->getId(), 'name', 'name'); if (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_ALL == $imageIncludePolicy) { - $this->_joinAttribute($store->getId(), 'thumbnail'); - $this->_select->columns( - [ - 'thumbnail' => $this->getConnection()->getIfNullSql( - 't2_thumbnail.value', - 't1_thumbnail.value' - ), - ] - ); + $this->_joinAttribute($store->getId(), 'thumbnail', 'thumbnail'); } elseif (\Magento\Sitemap\Model\Source\Product\Image\IncludeImage::INCLUDE_BASE == $imageIncludePolicy) { - $this->_joinAttribute($store->getId(), 'image'); - $this->_select->columns( - ['image' => $this->getConnection()->getIfNullSql('t2_image.value', 't1_image.value')] - ); + $this->_joinAttribute($store->getId(), 'image', 'image'); } } - $query = $connection->query($this->_select); + $query = $connection->query($this->prepareSelectStatement($this->_select)); while ($row = $query->fetch()) { $product = $this->_prepareProduct($row, $store->getId()); $products[$product->getId()] = $product; @@ -422,6 +419,17 @@ protected function _getMediaConfig() return $this->_mediaConfig; } + /** + * Allow to modify select statement with plugins + * + * @param \Magento\Framework\DB\Select $select + * @return \Magento\Framework\DB\Select + */ + public function prepareSelectStatement(\Magento\Framework\DB\Select $select) + { + return $select; + } + /** * Get product image URL from image filename and path *