diff --git a/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsDiscount.php b/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsDiscount.php index 553ef00a9..921e03d8d 100644 --- a/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsDiscount.php +++ b/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsDiscount.php @@ -8,12 +8,13 @@ * @category Smile * @package Smile\ElasticsuiteCatalogRule * @author Romain Ruaud - * @copyright 2020 Smile + * @copyright 2021 Smile * @license Open Software License ("OSL") v. 3.0 */ namespace Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute; use Smile\ElasticsuiteCatalogRule\Api\Rule\Condition\Product\SpecialAttributeInterface; +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; /** * Special "is_discount" attribute class. @@ -29,14 +30,31 @@ class IsDiscount implements SpecialAttributeInterface */ private $booleanSource; + /** + * @var \Magento\Customer\Model\Session + */ + private $customerSession; + + /** + * @var \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory + */ + private $queryFactory; + /** * IsDiscount constructor. * - * @param \Magento\Config\Model\Config\Source\Yesno $booleanSource Boolean Source + * @param \Magento\Config\Model\Config\Source\Yesno $booleanSource Boolean Source. + * @param \Magento\Customer\Model\Session $customerSession Customer session. + * @param \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory Query factory. */ - public function __construct(\Magento\Config\Model\Config\Source\Yesno $booleanSource) - { - $this->booleanSource = $booleanSource; + public function __construct( + \Magento\Config\Model\Config\Source\Yesno $booleanSource, + \Magento\Customer\Model\Session $customerSession, + \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory + ) { + $this->booleanSource = $booleanSource; + $this->customerSession = $customerSession; + $this->queryFactory = $queryFactory; } /** @@ -52,8 +70,31 @@ public function getAttributeCode() */ public function getSearchQuery() { - // Query can be computed directly with the attribute code and value. (price.is_discount = true). - return null; + /* + * Query cannot be computed directly with the attribute code and value (price.is_discount = true). + * The customer group needs to be taken into account because the discounted price could be a tiers price. + */ + return $this->queryFactory->create( + QueryInterface::TYPE_NESTED, + [ + 'path' => 'price', + 'query' => $this->queryFactory->create( + QueryInterface::TYPE_BOOL, + [ + 'must' => [ + $this->queryFactory->create( + QueryInterface::TYPE_TERM, + ['field' => 'price.customer_group_id', 'value' => $this->customerSession->getCustomerGroupId()] + ), + $this->queryFactory->create( + QueryInterface::TYPE_TERM, + ['field' => 'price.is_discount', 'value' => true] + ), + ], + ] + ) + ] + ); } /** diff --git a/src/module-elasticsuite-catalog-rule/etc/di.xml b/src/module-elasticsuite-catalog-rule/etc/di.xml index 0f28a4857..8c2452f07 100644 --- a/src/module-elasticsuite-catalog-rule/etc/di.xml +++ b/src/module-elasticsuite-catalog-rule/etc/di.xml @@ -11,7 +11,7 @@ * @category Smile * @package Smile\ElasticsuiteCatalogRule * @author Romain Ruaud - * @copyright 2020 Smile + * @copyright 2021 Smile * @license Open Software License ("OSL") v. 3.0 */ --> @@ -29,4 +29,11 @@ + + + + + Magento\Customer\Model\Session\Proxy + + diff --git a/src/module-elasticsuite-virtual-category/Helper/Rule.php b/src/module-elasticsuite-virtual-category/Helper/Rule.php index ad6445863..19cce20f1 100644 --- a/src/module-elasticsuite-virtual-category/Helper/Rule.php +++ b/src/module-elasticsuite-virtual-category/Helper/Rule.php @@ -8,7 +8,7 @@ * @category Smile * @package Smile\ElasticsuiteVirtualCategory * @author Romain Ruaud - * @copyright 2020 Smile + * @copyright 2021 Smile * @license Open Software License ("OSL") v. 3.0 */ @@ -30,14 +30,23 @@ class Rule */ private $cache; + /** + * @var \Magento\Customer\Model\Session + */ + private $customerSession; + /** * Provider constructor. * - * @param \Magento\Framework\App\CacheInterface $cache Cache + * @param \Magento\Framework\App\CacheInterface $cache Cache. + * @param \Magento\Customer\Model\Session $customerSession Customer session. */ - public function __construct(\Magento\Framework\App\CacheInterface $cache) - { + public function __construct( + \Magento\Framework\App\CacheInterface $cache, + \Magento\Customer\Model\Session $customerSession + ) { $this->cache = $cache; + $this->customerSession = $customerSession; } /** @@ -52,7 +61,7 @@ public function __construct(\Magento\Framework\App\CacheInterface $cache) public function loadUsingCache(CategoryInterface $category, $callback) { \Magento\Framework\Profiler::start('ES:Virtual Rule ' . $callback); - $cacheKey = implode('|', [$callback, $category->getStoreId(), $category->getId()]); + $cacheKey = implode('|', [$callback, $category->getStoreId(), $category->getId(), $this->customerSession->getCustomerGroupId()]); $data = $this->cache->load($cacheKey); diff --git a/src/module-elasticsuite-virtual-category/etc/di.xml b/src/module-elasticsuite-virtual-category/etc/di.xml index cf4232e88..b158be99f 100644 --- a/src/module-elasticsuite-virtual-category/etc/di.xml +++ b/src/module-elasticsuite-virtual-category/etc/di.xml @@ -11,7 +11,7 @@ * @category Smile * @package Smile\ElasticsuiteVirtualCategory * @author Aurelien FOUCRET - * @copyright 2020 Smile + * @copyright 2021 Smile * @license Open Software License ("OSL") v. 3.0 */ --> @@ -205,4 +205,11 @@ + + + + + Magento\Customer\Model\Session\Proxy + +