Skip to content

Commit 4775ed1

Browse files
authored
Merge pull request #1781 from algolia/feat/MAGE-1112-price-indexing
MAGE-1112: Make `price` attribute optional during product indexing
2 parents f900207 + d8b9631 commit 4775ed1

File tree

4 files changed

+78
-13
lines changed

4 files changed

+78
-13
lines changed

Helper/ConfigHelper.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,17 +1068,42 @@ public function getAttributesToRetrieve($groupId)
10681068
return ['attributesToRetrieve' => $attributes];
10691069
}
10701070

1071+
/**
1072+
* @param $attributes
1073+
* @param $attributeName
1074+
* @return bool
1075+
*/
1076+
public function isAttributeInList($attributes, $attributeName): bool
1077+
{
1078+
foreach ($attributes as $attr) {
1079+
if ($attr['attribute'] === $attributeName) {
1080+
return true;
1081+
}
1082+
}
1083+
1084+
return false;
1085+
}
1086+
10711087
/**
10721088
* @param $storeId
1073-
* @return array
1089+
* @return mixed
10741090
*/
1075-
public function getProductAdditionalAttributes($storeId = null)
1091+
public function getProductAttributesList($storeId = null)
10761092
{
1077-
$attributes = $this->serializer->unserialize($this->configInterface->getValue(
1093+
return $this->serializer->unserialize($this->configInterface->getValue(
10781094
self::PRODUCT_ATTRIBUTES,
10791095
ScopeInterface::SCOPE_STORE,
10801096
$storeId
10811097
));
1098+
}
1099+
1100+
/**
1101+
* @param $storeId
1102+
* @return array
1103+
*/
1104+
public function getProductAdditionalAttributes($storeId = null)
1105+
{
1106+
$attributes = $this->getProductAttributesList($storeId);
10821107

10831108
$facets = $this->serializer->unserialize($this->configInterface->getValue(
10841109
self::FACETS,

Helper/Configuration/NoticeHelper.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
2323
'getRecommendNotice',
2424
'getCookieConfigurationNotice',
2525
'getMultiApplicationIDsNotice',
26+
'getPriceIndexingNotice',
2627
];
2728

2829
/** @var array[] */
@@ -189,8 +190,8 @@ protected function getClickAnalyticsNotice()
189190
];
190191
}
191192

192-
protected function getCookieConfigurationNotice()
193-
{
193+
protected function getCookieConfigurationNotice()
194+
{
194195
$noticeContent = '';
195196
$selector = '';
196197
$method = 'after';
@@ -363,4 +364,33 @@ protected function getMultiApplicationIDsNotice(): void
363364
'message' => $this->formatNotice($noticeTitle, $noticeContent),
364365
];
365366
}
367+
368+
369+
/**
370+
* This notice serves as a warning when user removes the price attribute from the attributes list but it's still present either in the sortings or in the facets
371+
* @return void
372+
*/
373+
protected function getPriceIndexingNotice(): void
374+
{
375+
$attributesToIndex = $this->configHelper->getProductAdditionalAttributes();
376+
$attributesList = $this->configHelper->getProductAttributesList();
377+
378+
// we want to display the warning only if price is not present in the attribute list but is present somewhere else
379+
if (!($this->configHelper->isAttributeInList($attributesToIndex, 'price')
380+
&& !$this->configHelper->isAttributeInList($attributesList, 'price'))
381+
) {
382+
return;
383+
}
384+
385+
$noticeTitle = 'Price attribute indexing';
386+
$noticeContent = '<p>Price attribute has been removed from the product attributes list but is still present in the facets, sortings or custom rankings lists.</p>
387+
<p>If you want to remove prices from the product records, you need to remove them from those lists as well.</p>
388+
<p>If you want the prices to be included in the product records, you need to add the price attribute in the product attributes list in the "Products" section of the configuration.</p>';
389+
390+
$this->notices[] = [
391+
'selector' => '.entry-edit',
392+
'method' => 'before',
393+
'message' => $this->formatNotice($noticeTitle, $noticeContent),
394+
];
395+
}
366396
}

Service/Product/RecordBuilder.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ public function buildRecord(DataObject $entity): array
123123
}
124124
$subProducts = $this->getSubProducts($product);
125125
$customData = $this->addAdditionalAttributes($customData, $additionalAttributes, $product, $subProducts);
126-
$customData = $this->priceManager->addPriceDataByProductType($customData, $product, $subProducts);
126+
127+
if ($this->isPriceIndexingEnabled($additionalAttributes)) {
128+
$customData = $this->priceManager->addPriceDataByProductType($customData, $product, $subProducts);
129+
}
130+
127131
$transport = new DataObject($customData);
128132
$this->eventManager->dispatch(
129133
'algolia_subproducts_index',
@@ -186,13 +190,16 @@ protected function addAttribute($attribute, $defaultData, $customData, $addition
186190
*/
187191
public function isAttributeEnabled($additionalAttributes, $attributeName): bool
188192
{
189-
foreach ($additionalAttributes as $attr) {
190-
if ($attr['attribute'] === $attributeName) {
191-
return true;
192-
}
193-
}
193+
return $this->configHelper->isAttributeInList($additionalAttributes, $attributeName);
194+
}
194195

195-
return false;
196+
/**
197+
* @param array $additionalAttributes
198+
* @return bool
199+
*/
200+
protected function isPriceIndexingEnabled(array $additionalAttributes): bool
201+
{
202+
return $this->configHelper->isAttributeInList($additionalAttributes, 'price');
196203
}
197204

198205
/**
@@ -816,3 +823,4 @@ public function productIsInStock($product, $storeId): bool
816823
return $product->isSaleable() && $stockItem->getIsInStock();
817824
}
818825
}
826+

Test/Integration/Indexing/Product/ProductsIndexingTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testDefaultIndexableAttributes()
7070
'thumbnail_url',
7171
'image_url',
7272
'in_stock',
73-
'price',
73+
//'price', since version 3.17.0, the price attribute is not mandatory if it's not present in any attributes list
7474
'type_id',
7575
'algoliaLastUpdateAtCET',
7676
'categoryIds',
@@ -85,6 +85,8 @@ public function testDefaultIndexableAttributes()
8585
unset($hit[$attribute]);
8686
}
8787

88+
$this->assertArrayNotHasKey('price', $hit, 'Record has a price attribute but it should not');
89+
8890
$extraAttributes = implode(', ', array_keys($hit));
8991
$this->assertEmpty($hit, 'Extra products attributes (' . $extraAttributes . ') are indexed and should not be.');
9092
}

0 commit comments

Comments
 (0)