Skip to content

Commit

Permalink
Merge pull request #3286 from Smile-SA/feat-force-empty-category-if-v…
Browse files Browse the repository at this point in the history
…irtual-category-root-disabled

[VirtualCategories] Option to force empty category if virtual category root disabled
  • Loading branch information
rbayet authored Aug 18, 2024
2 parents 68ebcf7 + d82b6ac commit f05e82a
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/module-elasticsuite-virtual-category/Helper/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteVirtualCategory
* @author Pierre Gauthier <pierre.gauthier@smile.fr>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteVirtualCategory\Helper;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

/**
* Smile Elasticsuite virtual category config helper.
*
* @category Smile
* @package Smile\ElasticsuiteVirtualCategory
* @author Pierre Gauthier <pierre.gauthier@smile.fr>
*/
class Config
{
const XML_PATH_FORCE_ZERO_RESULTS_FOR_DISABLED_CATEGORIES = 'smile_elasticsuite_catalogsearch_settings/catalogsearch/' .
'force_zero_results_for_disabled_categories';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Constructor
*
* @param ScopeConfigInterface $scopeConfig Scope configuration.
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

/**
* Returns true if categories and search queries positions should be ignored for Out of Stock products.
*
* @param integer $storeId Store id
*
* @return bool
*/
public function isForceZeroResultsForDisabledCategoriesEnabled($storeId): bool
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_FORCE_ZERO_RESULTS_FOR_DISABLED_CATEGORIES,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
}
31 changes: 30 additions & 1 deletion src/module-elasticsuite-virtual-category/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Magento\Catalog\Api\Data\CategoryInterface;
use Smile\ElasticsuiteVirtualCategory\Api\Data\VirtualRuleInterface;
use Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\QueryBuilder;
use Smile\ElasticsuiteVirtualCategory\Helper\Config;
use Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\VirtualCategory\CollectionFactory;
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory;
use Magento\Catalog\Model\CategoryFactory;
Expand Down Expand Up @@ -87,6 +88,11 @@ class Rule extends \Smile\ElasticsuiteCatalogRule\Model\Rule implements VirtualR
*/
private $sharedCache;

/**
* @var Config
*/
private $config;

/**
* @var Category[]
*/
Expand Down Expand Up @@ -115,6 +121,7 @@ class Rule extends \Smile\ElasticsuiteCatalogRule\Model\Rule implements VirtualR
* @param StoreManagerInterface $storeManagerInterface Store Manager
* @param Session $customerSession Customer session.
* @param CacheInterface $cache Cache.
* @param Config $config Virtual category configuration.
* @param array $data Additional data.
*/
public function __construct(
Expand All @@ -132,6 +139,7 @@ public function __construct(
StoreManagerInterface $storeManagerInterface,
Session $customerSession,
CacheInterface $cache,
Config $config,
array $data = []
) {
$this->queryFactory = $queryFactory;
Expand All @@ -142,6 +150,7 @@ public function __construct(
$this->storeManager = $storeManagerInterface;
$this->customerSession = $customerSession;
$this->sharedCache = $cache;
$this->config = $config;

parent::__construct($context, $registry, $formFactory, $localeDate, $combineConditionsFactory, $conditionDataFactory, $data);
}
Expand Down Expand Up @@ -179,13 +188,15 @@ public function getCategorySearchQuery($category, $excludedCategories = []): ?Qu
{
\Magento\Framework\Profiler::start('ES:Virtual Rule ' . __FUNCTION__);
$categoryId = (int) (!is_object($category) ? $category : $category->getId());
$storeId = !is_object($category) ? $this->getStoreId() : $category->getStoreId();
$cacheKey = implode(
'|',
[
__FUNCTION__,
!is_object($category) ? $this->getStoreId() : $category->getStoreId(),
$storeId,
$categoryId,
$this->customerSession->getCustomerGroupId(),
$this->config->isForceZeroResultsForDisabledCategoriesEnabled($storeId),
]
);

Expand Down Expand Up @@ -306,6 +317,11 @@ private function buildCategorySearchQuery($category, $excludedCategories = []):
if (!in_array($category->getId(), $excludedCategories)) {
$excludedCategories[] = $category->getId();

if (!$category->getIsActive()
&& $this->config->isForceZeroResultsForDisabledCategoriesEnabled($this->getStoreId())) {
return $this->getNoResultsQuery();
}

if ((bool) $category->getIsVirtualCategory() && $category->getIsActive()) {
$query = $this->getVirtualCategoryQuery($category, $excludedCategories);
} elseif ($category->getId() && $category->getIsActive()) {
Expand Down Expand Up @@ -601,4 +617,17 @@ private function saveInLocalCache(int $categoryId, $query): void
self::$localCache[$categoryId] = $query;
}
}

/**
* Build a query that return zero products.
*
* @return QueryInterface
*/
private function getNoResultsQuery(): QueryInterface
{
return $this->queryFactory->create(
QueryInterface::TYPE_TERMS,
['field' => 'entity_id', 'values' => [0]]
);
}
}
34 changes: 34 additions & 0 deletions src/module-elasticsuite-virtual-category/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!--
/**
* Smil_ElasticsuiteCatalog module configuration
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Romain RUAUD <romain.ruaud@smile.fr>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="smile_elasticsuite_catalogsearch_settings" translate="label" type="text" sortOrder="1000" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Catalog Search</label>
<tab>smile_elasticsuite</tab>
<resource>Magento_Backend::smile_elasticsuite_catalogsearch</resource>

<group id="catalogsearch" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Catalog Search Configuration</label>
<field id="force_zero_results_for_disabled_categories" translate="label comment" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Force zero results for disabled categories</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If set to yes, any virtual category that references a disabled category in their rule or as their root category will return an empty result. Be careful modifying this option will remove all your category query cache.]]></comment>
</field>
</group>
</section>
</system>
</config>
26 changes: 26 additions & 0 deletions src/module-elasticsuite-virtual-category/etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<!--
/**
* Smil_ElasticsuiteCatalog module configuration
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @author Romain RUAUD <romain.ruaud@smile.fr>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<smile_elasticsuite_catalogsearch_settings>
<catalogsearch>
<force_zero_results_for_disabled_categories>1</force_zero_results_for_disabled_categories>
</catalogsearch>
</smile_elasticsuite_catalogsearch_settings>
</default>
</config>
2 changes: 2 additions & 0 deletions src/module-elasticsuite-virtual-category/i18n/en_US.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"Add/Remove Products","Add/Remove Products"
"Force zero results for disabled categories","Force zero results for disabled categories"
"If set to yes, any virtual category that references a disabled category in their rule or as their root category will return an empty result. Be careful modifying this option will remove all your category query cache.","If set to yes, any virtual category that references a disabled category in their rule or as their root category will return an empty result. Be careful modifying this option will remove all your category query cache."
"Virtual rule","Virtual rule"
"Virtual Category","Virtual Category"
"Virtual Category Root","Virtual Category Root"
Expand Down
2 changes: 2 additions & 0 deletions src/module-elasticsuite-virtual-category/i18n/fr_FR.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"Add/Remove Products","Ajouter/Supprimer des produits"
"Force zero results for disabled categories","Forcer un résultat vide pour les catégories désactivées"
"If set to yes, any virtual category that references a disabled category in their rule or as their root category will return an empty result. Be careful modifying this option will remove all your category query cache.","Si ce paramètre est activé, toutes catégories virtuelles qui référencent une catégorie désactivée dans leur règle ou en tant que catégorie racine retourneront un résultat vide. Attention, si vous modifiez cette option, cela va vider votre cache des requêtes de catégories."
"Virtual rule","Règle à appliquer"
"Virtual Category","Catégorie virtuelle"
"Virtual Category Root","Racine de la catégorie virtuelle"
Expand Down

0 comments on commit f05e82a

Please sign in to comment.