@@ -45,7 +45,7 @@ public function getAttributesForFaceting(int $storeId): array
45
45
function ($ facet ) {
46
46
return $ this ->decorateAttributeForFaceting ($ facet );
47
47
},
48
- $ this ->getRawFacets ($ storeId )
48
+ $ this ->addMerchandisingFacets ( $ storeId , $ this -> getRawFacets ($ storeId) )
49
49
);
50
50
}
51
51
@@ -107,14 +107,26 @@ protected function getRenderingContentAttributes(array $facets): array
107
107
return array_map (
108
108
function (string $ attribute ) {
109
109
if ($ attribute === self ::FACET_ATTRIBUTE_CATEGORIES ) {
110
- $ attribute = self :: FACET_ATTRIBUTE_CATEGORIES . ' .level0 ' ;
110
+ $ attribute = $ this -> getRenderingContentFriendlyCategoryFacetAttributeName () ;
111
111
}
112
112
return $ attribute ;
113
113
},
114
114
$ facets
115
115
);
116
116
}
117
117
118
+ /**
119
+ * `renderingContent` cannot utilize the entire categories object but instead must reference a scalar value
120
+ * Obtaining the root level of the category data will enable it to become selectable in the Algolia Dashboard
121
+ * for "Facet Display" and "Order facets" within merchandising rules
122
+ *
123
+ * @return string
124
+ */
125
+ protected function getRenderingContentFriendlyCategoryFacetAttributeName (): string
126
+ {
127
+ return self ::FACET_ATTRIBUTE_CATEGORIES . '.level0 ' ;
128
+ }
129
+
118
130
/**
119
131
* @param string $attribute
120
132
* @param bool $searchable
@@ -130,6 +142,7 @@ protected function getRawFacet(string $attribute, bool $searchable = false): arr
130
142
131
143
/**
132
144
* Generates common data to be used for both attributesForFaceting and renderingContent
145
+ *
133
146
* @return array<array<string, mixed>>
134
147
* @throws NoSuchEntityException
135
148
* @throws LocalizedException
@@ -155,26 +168,55 @@ function(string $attribute) {
155
168
}
156
169
}
157
170
158
- $ this ->facets [$ storeId ] = $ this ->addCategoryFacets ($ storeId , $ rawFacets );
171
+ $ this ->facets [$ storeId ] = $ this ->assertCategoryFacet ($ storeId , $ rawFacets );
172
+
159
173
return $ this ->facets [$ storeId ];
160
174
}
161
175
162
176
/**
177
+ * @param array<array<string, mixed>> $facets
178
+ * @return bool
179
+ */
180
+ protected function hasCategoryFacet (array $ facets ): bool
181
+ {
182
+ return !!array_filter ($ facets , function ($ facet ) {
183
+ return $ facet ['attribute ' ] === self ::FACET_ATTRIBUTE_CATEGORIES ;
184
+ });
185
+ }
186
+
187
+ /**
188
+ * Applies the category facet if not manually configured but necessary for category functionality
189
+ * (The presence of the category facet drives logic for attributesForFaceting and renderingContent)
190
+ *
163
191
* @param int $storeId
164
192
* @param array<array<string, mixed>> $facets
165
193
* @return array<array<string, mixed>>
166
194
*/
167
- protected function addCategoryFacets (int $ storeId , array $ facets ): array
195
+ protected function assertCategoryFacet (int $ storeId , array $ facets ): array
168
196
{
169
197
if ($ this ->configHelper ->replaceCategories ($ storeId )
170
- && !array_filter ($ facets , function ($ facet ) {
171
- return $ facet ['attribute ' ] === self ::FACET_ATTRIBUTE_CATEGORIES ;
172
- })
198
+ && !$ this ->hasCategoryFacet ($ facets )
173
199
) {
174
200
$ facets [] = $ this ->getRawFacet (self ::FACET_ATTRIBUTE_CATEGORIES );
175
201
}
176
202
177
- // Added for legacy merchandising features
203
+ return $ facets ;
204
+ }
205
+
206
+ /**
207
+ * Add merchandising facets as needed for attributesForFaceting
208
+ *
209
+ * @param int $storeId
210
+ * @param array<array<string, mixed>> $facets
211
+ * @return array|string[]
212
+ */
213
+ protected function addMerchandisingFacets (int $ storeId , array $ facets ): array
214
+ {
215
+ if ($ this ->hasCategoryFacet ($ facets )) {
216
+ $ facets [] = $ this ->getRawFacet ($ this ->getRenderingContentFriendlyCategoryFacetAttributeName ());
217
+ }
218
+
219
+ // Used for legacy merchandising features - always required!
178
220
$ facets [] = $ this ->getRawFacet (self ::FACET_ATTRIBUTES_CATEGORY_ID );
179
221
180
222
if ($ this ->configHelper ->isVisualMerchEnabled ($ storeId )) {
0 commit comments