Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "IsSaleable" special attribute for optimizers #2637

Open
wants to merge 9 commits into
base: 2.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalogRule
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @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');
}
}
1 change: 1 addition & 0 deletions src/module-elasticsuite-catalog-rule/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<arguments>
<argument name="attributes" xsi:type="array">
<item name="has_image" xsi:type="object">Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\HasImage</item>
<item name="is_saleable" xsi:type="object">Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\IsSaleable</item>
<item name="stock.is_in_stock" xsi:type="object">Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\IsInStock</item>
<item name="price.is_discount" xsi:type="object">Smile\ElasticsuiteCatalogRule\Model\Rule\Condition\Product\SpecialAttribute\IsDiscount</item>
<item name="is_bundle" xsi:type="object">isBundleProduct</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<field name="children_ids" type="integer" />
<field name="configurable_attributes" type="keyword" />
<field name="indexed_attributes" type="keyword" />
<field name="is_saleable" type="integer" />

<!-- Static fields handled by the "prices" datasource -->
<field name="price.price" type="double" nestedPath="price" />
Expand Down