Skip to content

Commit

Permalink
Merge pull request #24 from delyriand/fix_Smile-SA/elasticsuite-for-r…
Browse files Browse the repository at this point in the history
…etailer_5

Fix Smile-SA/elasticsuite-for-retailer#5 - 'Only discounted products'…
  • Loading branch information
Fdec authored Sep 4, 2018
2 parents 6d3dc5b + 253dc79 commit 19a0a20
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
134 changes: 134 additions & 0 deletions Model/Rule/Condition/Product/SpecialAttribute/IsDiscount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile\RetailerOffer
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @author Maxime Leclercq <maxime.leclercq@smile.fr>
* @copyright 2018 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\RetailerOffer\Model\Rule\Condition\Product\SpecialAttribute;

use Magento\Config\Model\Config\Source\Yesno;
use Smile\ElasticsuiteCatalogRule\Api\Rule\Condition\Product\SpecialAttributeInterface;
use Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\IsDiscount as BaseIsDiscount;
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
use Smile\StoreLocator\CustomerData\CurrentStore;
use Smile\RetailerOffer\Helper\Settings;

/**
* Is Discount rule condition.
* Override the virtual category discount rule condition for use offer data when in drive mode.
*
* @category Smile
* @package Smile\RetailerOffer
*/
class IsDiscount extends BaseIsDiscount implements SpecialAttributeInterface
{
/**
* @var QueryFactory
*/
private $queryFactory;

/**
* @var CurrentStore
*/
private $currentStore;

/**
* @var Settings
*/
private $settingsHelper;

/**
* IsDiscount constructor.
*
* @param Yesno $booleanSource Boolean source model
* @param QueryFactory $queryFactory Query factory
* @param CurrentStore $currentStore Current Store
* @param Settings $settingsHelper Setting Helper
*/
public function __construct(
Yesno $booleanSource,
QueryFactory $queryFactory,
CurrentStore $currentStore,
Settings $settingsHelper
) {
$this->queryFactory = $queryFactory;
$this->currentStore = $currentStore;
$this->settingsHelper = $settingsHelper;

parent::__construct($booleanSource);
}

/**
* {@inheritdoc}
*/
public function getSearchQuery()
{
$query = parent::getSearchQuery();

if ($this->settingsHelper->isDriveMode()) {
$query = $this->getIsDiscountOffersQuery();
$currentRetailer = $this->currentStore->getRetailer();
if ($currentRetailer && $currentRetailer->getId()) {
$query = $this->getIsDiscountForCurrentRetailerQuery($currentRetailer->getId());
}
}

return $query;
}

/**
* Retrieve query based on 'offer.is_discount' for current retailer.
*
* @param integer $retailerId Retailer Id
*
* @return QueryInterface
*/
private function getIsDiscountForCurrentRetailerQuery($retailerId)
{
$sellerIdFilter = $this->queryFactory->create(
QueryInterface::TYPE_TERM,
['field' => 'offer.seller_id', 'value' => $retailerId]
);
$mustClause = ['must' => [$sellerIdFilter]];
$mustClause['must'][] = $this->getIsDiscountOfferTermQuery();

return $this->queryFactory->create(
QueryInterface::TYPE_NESTED,
['path' => 'offer', 'query' => $this->queryFactory->create(QueryInterface::TYPE_BOOL, $mustClause)]
);
}

/**
* Filter products having a discounted offer.
*
* @return QueryInterface
*/
private function getIsDiscountOffersQuery()
{
return $this->queryFactory->create(
QueryInterface::TYPE_NESTED,
['path' => 'offer', 'query' => $this->getIsDiscountOfferTermQuery()]
);
}

/**
* Retrieve term query to match discounted offers.
*
* @return QueryInterface
*/
private function getIsDiscountOfferTermQuery()
{
return $this->queryFactory->create(
QueryInterface::TYPE_TERM,
['field' => 'offer.is_discount', 'value' => true]
);
}
}
9 changes: 9 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@
<argument name="offerCollectionFactory" xsi:type="object">Smile\RetailerOffer\Model\ResourceModel\RetailerOffer\CollectionFactory</argument>
</arguments>
</type>

<type name="Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttributesProvider">
<arguments>
<argument name="attributes" xsi:type="array">
<!-- Rewrite for price.is_discount to compute it on current retailer if needed. -->
<item name="price.is_discount" xsi:type="object">Smile\RetailerOffer\Model\Rule\Condition\Product\SpecialAttribute\IsDiscount</item>
</argument>
</arguments>
</type>
</config>
1 change: 1 addition & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module name="Smile_Offer" />
<module name="Smile_Retailer" />
<module name="Smile_StoreLocator" />
<module name="Smile_ElasticsuiteCatalogRule" />
</sequence>
</module>
</config>

0 comments on commit 19a0a20

Please sign in to comment.