Skip to content

Commit ae7c4d0

Browse files
committed
MAGE-1258 Add rule friendly category attribute and remove non relevant facets from renderingContent
1 parent 688b695 commit ae7c4d0

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

Service/Product/FacetBuilder.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getAttributesForFaceting(int $storeId): array
4545
function($facet) {
4646
return $this->decorateAttributeForFaceting($facet);
4747
},
48-
$this->getRawFacets($storeId)
48+
$this->addMerchandisingFacets($storeId, $this->getRawFacets($storeId))
4949
);
5050
}
5151

@@ -107,14 +107,26 @@ protected function getRenderingContentAttributes(array $facets): array
107107
return array_map(
108108
function(string $attribute) {
109109
if ($attribute === self::FACET_ATTRIBUTE_CATEGORIES) {
110-
$attribute = self::FACET_ATTRIBUTE_CATEGORIES . '.level0';
110+
$attribute = $this->getRenderingContentFriendlyCategoryFacetAttributeName();
111111
}
112112
return $attribute;
113113
},
114114
$facets
115115
);
116116
}
117117

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+
118130
/**
119131
* @param string $attribute
120132
* @param bool $searchable
@@ -130,6 +142,7 @@ protected function getRawFacet(string $attribute, bool $searchable = false): arr
130142

131143
/**
132144
* Generates common data to be used for both attributesForFaceting and renderingContent
145+
*
133146
* @return array<array<string, mixed>>
134147
* @throws NoSuchEntityException
135148
* @throws LocalizedException
@@ -155,26 +168,55 @@ function(string $attribute) {
155168
}
156169
}
157170

158-
$this->facets[$storeId] = $this->addCategoryFacets($storeId, $rawFacets);
171+
$this->facets[$storeId] = $this->assertCategoryFacet($storeId, $rawFacets);
172+
159173
return $this->facets[$storeId];
160174
}
161175

162176
/**
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+
*
163191
* @param int $storeId
164192
* @param array<array<string, mixed>> $facets
165193
* @return array<array<string, mixed>>
166194
*/
167-
protected function addCategoryFacets(int $storeId, array $facets): array
195+
protected function assertCategoryFacet(int $storeId, array $facets): array
168196
{
169197
if ($this->configHelper->replaceCategories($storeId)
170-
&& !array_filter($facets, function($facet) {
171-
return $facet['attribute'] === self::FACET_ATTRIBUTE_CATEGORIES;
172-
})
198+
&& !$this->hasCategoryFacet($facets)
173199
) {
174200
$facets[] = $this->getRawFacet(self::FACET_ATTRIBUTE_CATEGORIES);
175201
}
176202

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!
178220
$facets[] = $this->getRawFacet(self::FACET_ATTRIBUTES_CATEGORY_ID);
179221

180222
if ($this->configHelper->isVisualMerchEnabled($storeId)) {

0 commit comments

Comments
 (0)