5
5
*/
6
6
namespace Magento \Catalog \Ui \DataProvider \Product ;
7
7
8
+ use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
9
+ use Magento \Framework \Exception \LocalizedException ;
10
+ use Magento \Eav \Model \Entity \Attribute \AttributeInterface ;
11
+
8
12
/**
9
13
* Collection which is used for rendering product list in the backend.
10
14
*
@@ -25,4 +29,63 @@ protected function _productLimitationJoinPrice()
25
29
$ this ->_productLimitationFilters ->setUsePriceIndex (false );
26
30
return $ this ->_productLimitationPrice (true );
27
31
}
32
+
33
+ /**
34
+ * Add attribute filter to collection
35
+ *
36
+ * @param AttributeInterface|integer|string|array $attribute
37
+ * @param null|string|array $condition
38
+ * @param string $joinType
39
+ * @return $this
40
+ * @throws LocalizedException
41
+ */
42
+ public function addAttributeToFilter ($ attribute , $ condition = null , $ joinType = 'inner ' )
43
+ {
44
+ $ storeId = (int )$ this ->getStoreId ();
45
+ if ($ attribute === 'is_saleable '
46
+ || is_array ($ attribute )
47
+ || $ storeId !== $ this ->getDefaultStoreId ()
48
+ ) {
49
+ return parent ::addAttributeToFilter ($ attribute , $ condition , $ joinType );
50
+ }
51
+
52
+ if ($ attribute instanceof AttributeInterface) {
53
+ $ attributeModel = $ attribute ;
54
+ } else {
55
+ $ attributeModel = $ this ->getEntity ()->getAttribute ($ attribute );
56
+ if ($ attributeModel === false ) {
57
+ throw new LocalizedException (
58
+ __ ('Invalid attribute identifier for filter (%1) ' , get_class ($ attribute ))
59
+ );
60
+ }
61
+ }
62
+
63
+ if ($ attributeModel ->isScopeGlobal () || $ attributeModel ->getBackend ()->isStatic ()) {
64
+ return parent ::addAttributeToFilter ($ attribute , $ condition , $ joinType );
65
+ }
66
+
67
+ $ this ->addAttributeToFilterAllStores ($ attributeModel , $ condition );
68
+
69
+ return $ this ;
70
+ }
71
+
72
+ /**
73
+ * Add attribute to filter by all stores
74
+ *
75
+ * @param Attribute $attributeModel
76
+ * @param array $condition
77
+ * @return void
78
+ */
79
+ private function addAttributeToFilterAllStores (Attribute $ attributeModel , array $ condition ): void
80
+ {
81
+ $ tableName = $ this ->getTable ($ attributeModel ->getBackendTable ());
82
+ $ entity = $ this ->getEntity ();
83
+ $ fKey = 'e. ' . $ this ->getEntityPkName ($ entity );
84
+ $ pKey = $ tableName . '. ' . $ this ->getEntityPkName ($ entity );
85
+ $ condition = "( {$ pKey } = {$ fKey }) AND ( "
86
+ . $ this ->_getConditionSql ("{$ tableName }.value " , $ condition )
87
+ . ') ' ;
88
+ $ selectExistsInAllStores = $ this ->getConnection ()->select ()->from ($ tableName );
89
+ $ this ->getSelect ()->exists ($ selectExistsInAllStores , $ condition );
90
+ }
28
91
}
0 commit comments