-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from delyriand/fix_Smile-SA/elasticsuite-for-r…
…etailer_5 Fix Smile-SA/elasticsuite-for-retailer#5 - 'Only discounted products'…
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
Model/Rule/Condition/Product/SpecialAttribute/IsDiscount.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters