diff --git a/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsInStock.php b/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsInStock.php index 12b0c1e06..a7b92276f 100644 --- a/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsInStock.php +++ b/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsInStock.php @@ -13,8 +13,11 @@ */ namespace Smile\ElasticsuiteCatalogRule\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 as ProductCondition; +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; +use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; /** * Special "is_in_stock" attribute class. @@ -26,18 +29,27 @@ class IsInStock implements SpecialAttributeInterface { /** - * @var \Magento\Config\Model\Config\Source\Yesno + * @var Yesno */ private $booleanSource; + /** + * @var QueryFactory + */ + private QueryFactory $queryFactory; + /** * IsInStock constructor. * - * @param \Magento\Config\Model\Config\Source\Yesno $booleanSource Boolean Source + * @param Yesno $booleanSource Boolean Source + * @param QueryFactory $queryFactory Query Factory */ - public function __construct(\Magento\Config\Model\Config\Source\Yesno $booleanSource) - { + public function __construct( + Yesno $booleanSource, + QueryFactory $queryFactory + ) { $this->booleanSource = $booleanSource; + $this->queryFactory = $queryFactory; } /** @@ -54,8 +66,19 @@ public function getAttributeCode() */ public function getSearchQuery(ProductCondition $condition) { - // Query can be computed directly with the attribute code and value. (stock.is_in_stock = true). - return null; + $queryParams = []; + + $queryParams[] = $this->queryFactory->create( + QueryInterface::TYPE_RANGE, + ['bounds' => ['gt' => (float) 0], 'field' => 'stock.qty'] + ); + + $queryParams[] = $this->queryFactory->create( + QueryInterface::TYPE_TERM, + ['value' => true, 'field' => 'stock.is_in_stock'] + ); + + return $this->queryFactory->create(QueryInterface::TYPE_BOOL, ['must' => $queryParams]); } /** diff --git a/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsSaleable.php b/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsSaleable.php new file mode 100644 index 000000000..1f67205ba --- /dev/null +++ b/src/module-elasticsuite-catalog-rule/Model/Rule/Condition/Product/SpecialAttribute/IsSaleable.php @@ -0,0 +1,131 @@ + + * @copyright 2020 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticsuiteCatalogRule\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 as ProductCondition; +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; +use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; + +/** + * Special "is_saleable" attribute class. + * + * @category Smile + * @package Smile\ElasticsuiteCatalogRule + */ +class IsSaleable implements SpecialAttributeInterface +{ + /** + * @var Yesno + */ + private $booleanSource; + + /** + * @var QueryFactory + */ + private QueryFactory $queryFactory; + + /** + * IsSaleable constructor. + * + * @param Yesno $booleanSource Boolean Source + * @param QueryFactory $queryFactory Query Factory + */ + public function __construct( + Yesno $booleanSource, + QueryFactory $queryFactory + ) { + $this->booleanSource = $booleanSource; + $this->queryFactory = $queryFactory; + } + + /** + * {@inheritdoc} + */ + public function getAttributeCode() + { + return 'is_saleable'; + } + + /** + * {@inheritdoc} + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function getSearchQuery(ProductCondition $condition) + { + return $this->queryFactory->create( + QueryInterface::TYPE_TERM, + ['value' => true, 'field' => 'stock.is_in_stock'] + ); + } + + /** + * {@inheritdoc} + */ + public function getOperatorName() + { + return ' '; + } + + /** + * {@inheritdoc} + */ + public function getInputType() + { + return 'select'; + } + + /** + * {@inheritdoc} + */ + public function getValueElementType() + { + return 'hidden'; + } + + /** + * {@inheritdoc} + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function getValueName($value) + { + return ' '; + } + + /** + * {@inheritdoc} + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function getValue($value) + { + return true; + } + + /** + * {@inheritdoc} + */ + public function getValueOptions() + { + return $this->booleanSource->toOptionArray(); + } + + /** + * {@inheritdoc} + */ + public function getLabel() + { + return __('Only saleable products'); + } +} diff --git a/src/module-elasticsuite-catalog-rule/etc/di.xml b/src/module-elasticsuite-catalog-rule/etc/di.xml index 4cc808cbd..507b5c541 100644 --- a/src/module-elasticsuite-catalog-rule/etc/di.xml +++ b/src/module-elasticsuite-catalog-rule/etc/di.xml @@ -22,6 +22,7 @@ Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\HasImage + Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\IsSaleable Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\IsInStock Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\IsDiscount isBundleProduct diff --git a/src/module-elasticsuite-catalog/etc/elasticsuite_indices.xml b/src/module-elasticsuite-catalog/etc/elasticsuite_indices.xml index f956b1669..4d37c2795 100644 --- a/src/module-elasticsuite-catalog/etc/elasticsuite_indices.xml +++ b/src/module-elasticsuite-catalog/etc/elasticsuite_indices.xml @@ -40,6 +40,7 @@ +