@@ -53,7 +53,7 @@ public function __construct(
53
53
public function apply (\Magento \Framework \App \RequestInterface $ request )
54
54
{
55
55
$ attributeValue = $ request ->getParam ($ this ->_requestVar );
56
- if (empty ($ attributeValue )) {
56
+ if (empty ($ attributeValue ) && ! is_numeric ( $ attributeValue ) ) {
57
57
return $ this ;
58
58
}
59
59
$ attribute = $ this ->getAttributeModel ();
@@ -84,9 +84,10 @@ protected function _getItemsData()
84
84
->getProductCollection ();
85
85
$ optionsFacetedData = $ productCollection ->getFacetedData ($ attribute ->getAttributeCode ());
86
86
87
- if (count ($ optionsFacetedData ) === 0
88
- && $ this ->getAttributeIsFilterable ($ attribute ) !== static ::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
89
- ) {
87
+ $ isAttributeFilterable =
88
+ $ this ->getAttributeIsFilterable ($ attribute ) === static ::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS ;
89
+
90
+ if (count ($ optionsFacetedData ) === 0 && !$ isAttributeFilterable ) {
90
91
return $ this ->itemDataBuilder ->build ();
91
92
}
92
93
@@ -95,28 +96,64 @@ protected function _getItemsData()
95
96
$ options = $ attribute ->getFrontend ()
96
97
->getSelectOptions ();
97
98
foreach ($ options as $ option ) {
98
- if (empty ($ option ['value ' ])) {
99
- continue ;
100
- }
99
+ $ this ->buildOptionData ($ option , $ isAttributeFilterable , $ optionsFacetedData , $ productSize );
100
+ }
101
101
102
- $ value = $ option ['value ' ];
102
+ return $ this ->itemDataBuilder ->build ();
103
+ }
103
104
104
- $ count = isset ($ optionsFacetedData [$ value ]['count ' ])
105
- ? (int )$ optionsFacetedData [$ value ]['count ' ]
106
- : 0 ;
107
- // Check filter type
108
- if ($ this ->getAttributeIsFilterable ($ attribute ) === static ::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
109
- && (!$ this ->isOptionReducesResults ($ count , $ productSize ) || $ count === 0 )
110
- ) {
111
- continue ;
112
- }
113
- $ this ->itemDataBuilder ->addItemData (
114
- $ this ->tagFilter ->filter ($ option ['label ' ]),
115
- $ value ,
116
- $ count
117
- );
105
+ /**
106
+ * Build option data
107
+ *
108
+ * @param array $option
109
+ * @param boolean $isAttributeFilterable
110
+ * @param array $optionsFacetedData
111
+ * @param int $productSize
112
+ * @return void
113
+ */
114
+ private function buildOptionData ($ option , $ isAttributeFilterable , $ optionsFacetedData , $ productSize )
115
+ {
116
+ $ value = $ this ->getOptionValue ($ option );
117
+ if ($ value === false ) {
118
+ return ;
119
+ }
120
+ $ count = $ this ->getOptionCount ($ value , $ optionsFacetedData );
121
+ if ($ isAttributeFilterable && (!$ this ->isOptionReducesResults ($ count , $ productSize ) || $ count === 0 )) {
122
+ return ;
118
123
}
119
124
120
- return $ this ->itemDataBuilder ->build ();
125
+ $ this ->itemDataBuilder ->addItemData (
126
+ $ this ->tagFilter ->filter ($ option ['label ' ]),
127
+ $ value ,
128
+ $ count
129
+ );
130
+ }
131
+
132
+ /**
133
+ * Retrieve option value if it exists
134
+ *
135
+ * @param array $option
136
+ * @return bool|string
137
+ */
138
+ private function getOptionValue ($ option )
139
+ {
140
+ if (empty ($ option ['value ' ]) && !is_numeric ($ option ['value ' ])) {
141
+ return false ;
142
+ }
143
+ return $ option ['value ' ];
144
+ }
145
+
146
+ /**
147
+ * Retrieve count of the options
148
+ *
149
+ * @param int|string $value
150
+ * @param array $optionsFacetedData
151
+ * @return int
152
+ */
153
+ private function getOptionCount ($ value , $ optionsFacetedData )
154
+ {
155
+ return isset ($ optionsFacetedData [$ value ]['count ' ])
156
+ ? (int )$ optionsFacetedData [$ value ]['count ' ]
157
+ : 0 ;
121
158
}
122
159
}
0 commit comments