99 */
1010namespace Magento \CatalogWidget \Model \Rule \Condition ;
1111
12- use Magento \Catalog \Api \Data \ProductInterface ;
1312use Magento \Catalog \Model \ProductCategoryList ;
1413use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14+ use Magento \Framework \DB \Select ;
1515use Magento \Store \Model \Store ;
1616
1717/**
@@ -201,7 +201,7 @@ protected function addNotGlobalAttribute(
201201 \Magento \Catalog \Model \ResourceModel \Eav \Attribute $ attribute ,
202202 Collection $ collection
203203 ) {
204- $ storeId = $ this ->storeManager ->getStore ()->getId ();
204+ /* $storeId = $this->storeManager->getStore()->getId();
205205 $values = $collection->getAllAttributeValues($attribute);
206206 $validEntities = [];
207207 if ($values) {
@@ -227,6 +227,56 @@ protected function addNotGlobalAttribute(
227227 $this->unsetData('value');
228228 }
229229
230+ return $this;*/
231+
232+ $ connection = $ this ->_productResource ->getConnection ();
233+ switch ($ attribute ->getBackendType ()) {
234+ case 'decimal ' :
235+ case 'datetime ' :
236+ case 'int ' :
237+ case 'varchar ' :
238+ case 'text ' :
239+ $ aliasDefault = 'at_ ' . $ attribute ->getAttributeCode () . '_default ' ;
240+ $ aliasStore = 'at_ ' . $ attribute ->getAttributeCode ();
241+ $ collection ->addAttributeToSelect ($ attribute ->getAttributeCode (), 'left ' );
242+ break ;
243+ default :
244+ $ aliasDefault = 'at_ ' . sha1 ($ this ->getId ()) . $ attribute ->getAttributeCode () . '_default ' ;
245+ $ aliasStore = 'at_ ' . sha1 ($ this ->getId ()) . $ attribute ->getAttributeCode ();
246+
247+ $ storeDefaultId = $ connection ->getIfNullSql ($ aliasDefault . '.store_id ' , Store::DEFAULT_STORE_ID );
248+ $ storeId = $ connection ->getIfNullSql ($ aliasStore . '.store_id ' , $ this ->storeManager ->getStore ()->getId ());
249+ $ linkField = $ attribute ->getEntity ()->getLinkField ();
250+
251+ $ collection ->getSelect ()->joinLeft (
252+ [$ aliasDefault => $ collection ->getTable ($ attribute ->getBackendTable ())],
253+ "( $ aliasDefault. $ linkField = e. $ linkField) AND ( $ aliasDefault.store_id = $ storeDefaultId) " .
254+ " AND ( $ aliasDefault.attribute_id = {$ attribute ->getId ()}) " ,
255+ []
256+ );
257+ $ collection ->getSelect ()->joinLeft (
258+ [$ aliasStore => $ collection ->getTable ($ attribute ->getBackendTable ())],
259+ "( $ aliasStore. $ linkField = e. $ linkField) AND ( $ aliasStore.store_id = $ storeId) " .
260+ " AND ( $ aliasStore.attribute_id = {$ attribute ->getId ()}) " ,
261+ []
262+ );
263+ }
264+
265+ $ fromPart = $ collection ->getSelect ()->getPart (Select::FROM );
266+ if (isset ($ fromPart ['search_result ' ]['joinType ' ])
267+ && $ fromPart ['search_result ' ]['joinType ' ] == Select::LEFT_JOIN
268+ ) {
269+ $ conditionCheck = $ connection ->quoteIdentifier ($ aliasStore . '.value_id ' ) . " > 0 " ;
270+ $ conditionTrue = $ connection ->quoteIdentifier ($ aliasStore . '.value ' );
271+ $ conditionFalse = $ connection ->quoteIdentifier ($ aliasDefault . '.value ' );
272+ $ ifSql = "IF ( $ conditionCheck, $ conditionTrue, $ conditionFalse) " ;
273+ $ joinedAttribute = new \Zend_Db_Expr ($ ifSql );
274+ } else {
275+ $ joinedAttribute = $ aliasStore . '. ' . 'value ' ;
276+ }
277+
278+ $ this ->joinedAttributes [$ attribute ->getAttributeCode ()] = $ joinedAttribute ;
279+
230280 return $ this ;
231281 }
232282
0 commit comments