77
88namespace Magento \ProductPriceDataExporter \Model \Query ;
99
10- use Magento \Catalog \Model \Product ;
1110use Magento \Catalog \Model \Product \Type ;
1211use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
13- use Magento \DataExporter \Exception \UnableRetrieveData ;
14- use Magento \Eav \Model \Config ;
1512use Magento \Framework \App \ResourceConnection ;
1613use Magento \Framework \DB \Select ;
1714use Magento \Framework \DB \Sql \Expression ;
@@ -32,68 +29,34 @@ class ProductPricesQuery
3229 */
3330 private MetadataPool $ metadataPool ;
3431
35- /**
36- * @var Config
37- */
38- private Config $ eavConfig ;
39-
40- /**
41- * @var array|null
42- */
43- private ?array $ priceAttributes = null ;
44-
4532 private const IGNORED_TYPES = [Configurable::TYPE_CODE , Type::TYPE_BUNDLE ];
4633
4734 /**
4835 * @param ResourceConnection $resourceConnection
4936 * @param MetadataPool $metadataPool
50- * @param Config $eavConfig
5137 */
5238 public function __construct (
5339 ResourceConnection $ resourceConnection ,
5440 MetadataPool $ metadataPool ,
55- Config $ eavConfig
5641 ) {
5742 $ this ->resourceConnection = $ resourceConnection ;
5843 $ this ->metadataPool = $ metadataPool ;
59- $ this ->eavConfig = $ eavConfig ;
6044 }
6145
6246 /**
63- * @inheritDoc
64- * @throws UnableRetrieveData
47+ * @param array $productIds
48+ * @param array $priceAttributes
49+ * @return Select
50+ * @throws \Exception
6551 */
66- public function getQuery (array $ productIds ): Select
52+ public function getQuery (array $ productIds, array $ priceAttributes = [] ): Select
6753 {
6854 /** @var \Magento\Framework\EntityManager\EntityMetadataInterface $metadata */
6955 $ metadata = $ this ->metadataPool ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class);
7056 $ connection = $ this ->resourceConnection ->getConnection ();
71-
72-
7357 $ eavAttributeTable = $ this ->resourceConnection ->getTableName ('catalog_product_entity_decimal ' );
7458 $ linkField = $ metadata ->getLinkField ();
7559
76- if ($ this ->priceAttributes === null ) {
77- $ attribute = $ this ->eavConfig ->getAttribute (Product::ENTITY , 'price ' );
78- if ($ attribute ) {
79- $ this ->priceAttributes ['price ' ] = $ attribute ->getId ();
80- }
81- $ attribute = $ this ->eavConfig ->getAttribute (Product::ENTITY , 'special_price ' );
82- if ($ attribute ) {
83- $ this ->priceAttributes ['special_price ' ] = $ attribute ->getId ();
84- }
85- }
86- if (!$ this ->priceAttributes ) {
87- throw new UnableRetrieveData ('Price attributes not found ' );
88- }
89-
90- $ priceAttributeCondition = \sprintf (
91- "CASE WHEN eav.attribute_id = %s THEN 'price'
92- WHEN eav.attribute_id = %s THEN 'special_price' ELSE 'price' END " ,
93- $ this ->priceAttributes ['price ' ],
94- $ this ->priceAttributes ['special_price ' ]
95- );
96-
9760 return $ connection ->select ()
9861 ->from (
9962 ['product ' => $ this ->resourceConnection ->getTableName ('catalog_product_entity ' )],
@@ -122,18 +85,23 @@ public function getQuery(array $productIds): Select
12285 ->joinLeft (
12386 ['eav ' => $ eavAttributeTable ],
12487 \sprintf ('product.%1$s = eav.%1$s ' , $ linkField ) .
125- $ connection ->quoteInto (' AND eav.attribute_id IN (?) ' , \array_values ( $ this -> priceAttributes ) ) .
88+ $ connection ->quoteInto (' AND eav.attribute_id IN (?) ' , $ priceAttributes ) .
12689 ' AND eav.store_id = 0 ' ,
127- [' attribute_id ' ]
90+ []
12891 )
12992 ->joinLeft (
13093 ['eav_store ' => $ eavAttributeTable ],
13194 \sprintf ('product.%1$s = eav_store.%1$s ' , $ linkField ) .
132- ' AND eav_store.store_id = sg.default_store_id AND eav_store.attribute_id = eav.attribute_id ' ,
133- ['price ' => new Expression (
134- 'CASE WHEN eav_store.value_id IS NOT NULL THEN eav_store.value WHEN eav.value '
135- . 'IS NOT NULL THEN eav.value ELSE 0 END '
136- )]
95+ $ connection ->quoteInto (' AND eav_store.attribute_id IN (?) ' , $ priceAttributes ) .
96+ ' AND eav_store.store_id = sg.default_store_id ' ,
97+ [
98+ 'price ' => new Expression (
99+ 'IF (eav_store.value_id, eav_store.value, eav.value) '
100+ ),
101+ 'attributeId ' => new Expression (
102+ 'IF(eav_store.value_id, eav_store.attribute_id, eav.attribute_id) '
103+ )
104+ ]
137105 )
138106 // get parent skus
139107 ->joinLeft (
@@ -157,15 +125,14 @@ public function getQuery(array $productIds): Select
157125 'parent_sku.entity_id = parent_website.product_id ' ,
158126 [
159127 'parent_skus ' => new Expression (
160- "GROUP_CONCAT(DISTINCT parent_sku.type_id,':',parent_sku.sku separator ', ') "
128+ "GROUP_CONCAT(DISTINCT parent_sku.type_id,':',parent_sku.sku separator ', ') "
161129 )
162130 ]
163131 )
164- ->columns (['price_attribute ' => new Expression ($ priceAttributeCondition )])
165132 ->where ('product.entity_id IN (?) ' , $ productIds )
166133 ->where ('product.type_id NOT IN (?) ' , self ::IGNORED_TYPES )
167134 ->group ('product.entity_id ' )
168135 ->group ('product_website.website_id ' )
169- ->group ('eav.attribute_id ' );
136+ ->group ('attributeId ' );
170137 }
171138}
0 commit comments