Skip to content

Commit

Permalink
Merge pull request #2399 from rbayet/fix_2380_is_discount_w_customer_…
Browse files Browse the repository at this point in the history
…group

Fixes #2380 is_discount tiers price support (2.8.x)
  • Loading branch information
rbayet authored Dec 17, 2021
2 parents 68285be + 169f3dd commit 16b8d5d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
* @category Smile
* @package Smile\ElasticsuiteCatalogRule
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @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.
Expand All @@ -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;
}

/**
Expand All @@ -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]
),
],
]
)
]
);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/module-elasticsuite-catalog-rule/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @category Smile
* @package Smile\ElasticsuiteCatalogRule
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2020 Smile
* @copyright 2021 Smile
* @license Open Software License ("OSL") v. 3.0
*/
-->
Expand All @@ -29,4 +29,11 @@
</type>

<preference for="Smile\ElasticsuiteCatalogRule\Api\Data\ConditionInterface" type="Smile\ElasticsuiteCatalogRule\Model\Data\Condition"/>

<!-- Session proxies -->
<type name="Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\IsDiscount">
<arguments>
<argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session\Proxy</argument>
</arguments>
</type>
</config>
19 changes: 14 additions & 5 deletions src/module-elasticsuite-virtual-category/Helper/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @category Smile
* @package Smile\ElasticsuiteVirtualCategory
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2020 Smile
* @copyright 2021 Smile
* @license Open Software License ("OSL") v. 3.0
*/

Expand All @@ -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;
}

/**
Expand All @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion src/module-elasticsuite-virtual-category/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @category Smile
* @package Smile\ElasticsuiteVirtualCategory
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
* @copyright 2020 Smile
* @copyright 2021 Smile
* @license Open Software License ("OSL") v. 3.0
*/
-->
Expand Down Expand Up @@ -205,4 +205,11 @@
<preference for="Smile\ElasticsuiteCatalog\Model\Category\Filter\Provider" type="Smile\ElasticsuiteVirtualCategory\Model\Category\Filter\Provider"/>

<preference for="Smile\ElasticsuiteVirtualCategory\Api\Data\VirtualRuleInterface" type="Smile\ElasticsuiteVirtualCategory\Model\Rule"/>

<!-- Session proxies -->
<type name="Smile\ElasticsuiteVirtualCategory\Helper\Rule">
<arguments>
<argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session\Proxy</argument>
</arguments>
</type>
</config>

0 comments on commit 16b8d5d

Please sign in to comment.