Skip to content

Commit 8e68283

Browse files
authored
Merge pull request #1565 from algolia/feature/MAGE-878
Feature/mage 878
2 parents 537c32b + fe464ac commit 8e68283

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

Helper/ConfigHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class ConfigHelper
9494
public const MAKE_SEO_REQUEST = 'algoliasearch_advanced/advanced/make_seo_request';
9595
public const REMOVE_BRANDING = 'algoliasearch_advanced/advanced/remove_branding';
9696
public const AUTOCOMPLETE_SELECTOR = 'algoliasearch_autocomplete/autocomplete/autocomplete_selector';
97+
public const INCLUDE_NON_VISIBLE_PRODUCTS_IN_INDEX = 'algoliasearch_products/products/include_non_visible_products_in_index';
9798
public const IDX_PRODUCT_ON_CAT_PRODUCTS_UPD = 'algoliasearch_advanced/advanced/index_product_on_category_products_update';
9899
public const PREVENT_BACKEND_RENDERING = 'algoliasearch_advanced/advanced/prevent_backend_rendering';
99100
public const PREVENT_BACKEND_RENDERING_DISPLAY_MODE =
@@ -234,6 +235,21 @@ public function getAutocompleteSelector($storeId = null)
234235
return $this->configInterface->getValue(self::AUTOCOMPLETE_SELECTOR, ScopeInterface::SCOPE_STORE, $storeId);
235236
}
236237

238+
/**
239+
* Returns config flag
240+
*
241+
* @param $storeId
242+
* @return bool
243+
*/
244+
public function includeNonVisibleProductsInIndex($storeId = null): bool
245+
{
246+
return $this->configInterface->isSetFlag(
247+
self::INCLUDE_NON_VISIBLE_PRODUCTS_IN_INDEX,
248+
ScopeInterface::SCOPE_STORE,
249+
$storeId
250+
);
251+
}
252+
237253
/**
238254
* @param $storeId
239255
* @return mixed

Helper/Data.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ public function rebuildStoreProductIndex(int $storeId, array $productIds): void
376376
$this->logger->start('Indexing');
377377
try {
378378
$this->logger->start('ok');
379-
$collection = $this->productHelper->getProductCollectionQuery($storeId, $productIds);
379+
$onlyVisible = !$this->configHelper->includeNonVisibleProductsInIndex($storeId);
380+
$collection = $this->productHelper->getProductCollectionQuery($storeId, $productIds, $onlyVisible);
380381
$size = $collection->getSize();
381382
if (!empty($productIds)) {
382383
$size = max(count($productIds), $size);
@@ -420,7 +421,8 @@ public function rebuildProductIndex(int $storeId, ?array $productIds, int $page,
420421
if ($this->isIndexingEnabled($storeId) === false) {
421422
return;
422423
}
423-
$collection = $this->productHelper->getProductCollectionQuery($storeId, null, $useTmpIndex);
424+
$onlyVisible = !$this->configHelper->includeNonVisibleProductsInIndex($storeId);
425+
$collection = $this->productHelper->getProductCollectionQuery($storeId, null, $onlyVisible);
424426
$this->rebuildStoreProductIndexPage($storeId, $collection, $page, $pageSize, null, $productIds, $useTmpIndex);
425427
}
426428

@@ -927,7 +929,8 @@ public function getIndexDataByStoreIds(): array
927929
*/
928930
protected function deleteInactiveIds($storeId, $objectIds, $indexName): void
929931
{
930-
$collection = $this->productHelper->getProductCollectionQuery($storeId, $objectIds);
932+
$onlyVisible = !$this->configHelper->includeNonVisibleProductsInIndex($storeId);
933+
$collection = $this->productHelper->getProductCollectionQuery($storeId, $objectIds, $onlyVisible);
931934
$dbIds = $collection->getAllIds();
932935
$collection = null;
933936
$idsToDeleteFromAlgolia = array_diff($objectIds, $dbIds);

Model/Indexer/Product.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public function execute($productIds)
9595
}
9696

9797
$useTmpIndex = $this->configHelper->isQueueActive($storeId);
98-
$collection = $this->productHelper->getProductCollectionQuery($storeId, $productIds, $useTmpIndex);
98+
$onlyVisible = !$this->configHelper->includeNonVisibleProductsInIndex();
99+
$collection = $this->productHelper->getProductCollectionQuery($storeId, $productIds, $onlyVisible);
99100
$size = $collection->getSize();
100101

101102
$pages = ceil($size / $productsPerPage);

ViewModel/Recommend/Cart.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public function getAllCartItems()
6464
$cartItems[] = $item->getProductId();
6565
}
6666
$storeId = $this->storeManager->getStore()->getId();
67-
$cartProductCollection = $this->productHelper->getProductCollectionQuery($storeId, array_unique($cartItems));
67+
$onlyVisible = !$this->configHelper->includeNonVisibleProductsInIndex();
68+
$cartProductCollection = $this->productHelper->getProductCollectionQuery($storeId, array_unique($cartItems), $onlyVisible);
6869
if ($cartProductCollection->getSize() > 0 ){
6970
foreach ($cartProductCollection as $product) {
7071
$visibleCartItem[] = $product->getId();

etc/adminhtml/system.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,13 @@
580580
]]>
581581
</comment>
582582
</field>
583+
<field id="include_non_visible_products_in_index" translate="label comment" type="select" sortOrder="55" showInDefault="1" showInWebsite="1" showInStore="1">
584+
<label>Include non visible products in index</label>
585+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
586+
<comment>
587+
Include non visible products in index. Default value is <strong>No</strong>
588+
</comment>
589+
</field>
583590
<field id="category_page_id_attribute_name" translate="label comment" type="text" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
584591
<label>Category page ID attribute</label>
585592
<comment>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<custom_ranking_product_attributes><![CDATA[{"_1600352070901_901":{"attribute":"in_stock","order":"desc"},"_1600352075148_148":{"attribute":"ordered_qty","order":"desc"},"_1600352080300_300":{"attribute":"created_at","order":"desc"}}]]></custom_ranking_product_attributes>
4747
<enable_visual_merchandising>0</enable_visual_merchandising>
4848
<category_page_id_attribute_name>categoryPageId</category_page_id_attribute_name>
49+
<include_non_visible_products_in_index>0</include_non_visible_products_in_index>
4950
</products>
5051
</algoliasearch_products>
5152
<algoliasearch_categories>

0 commit comments

Comments
 (0)