diff --git a/src/module-elasticsuite-catalog/Helper/ProductAttribute.php b/src/module-elasticsuite-catalog/Helper/ProductAttribute.php index b5e794c09..adcdaad58 100644 --- a/src/module-elasticsuite-catalog/Helper/ProductAttribute.php +++ b/src/module-elasticsuite-catalog/Helper/ProductAttribute.php @@ -117,12 +117,12 @@ public function getFieldType(ProductAttributeInterface $attribute) { $type = FieldInterface::FIELD_TYPE_STRING; - if ($attribute->getBackendType() == 'int' || $attribute->getFrontendClass() == 'validate-digits') { + if ($attribute->getSourceModel() == 'Magento\Eav\Model\Entity\Attribute\Source\Boolean') { + $type = FieldInterface::FIELD_TYPE_BOOLEAN; + } elseif ($attribute->getBackendType() == 'int' || $attribute->getFrontendClass() == 'validate-digits') { $type = FieldInterface::FIELD_TYPE_INTEGER; } elseif ($attribute->getBackendType() == 'decimal' || $attribute->getFrontendClass() == 'validate-number') { $type = FieldInterface::FIELD_TYPE_DOUBLE; - } elseif ($attribute->getSourceModel() == 'eav/entity_attribute_source_boolean') { - $type = FieldInterface::FIELD_TYPE_BOOLEAN; } elseif ($attribute->getBackendType() == 'datetime') { $type = FieldInterface::FIELD_TYPE_DATE; } elseif ($attribute->usesSource() && $attribute->getSourceModel() === null) { @@ -221,6 +221,9 @@ public function getIndexOptionText(ProductAttributeInterface $attribute, $storeI if (!isset($this->attributeOptionTextCache[$storeId][$attributeId][$optionId])) { $optionValue = $attribute->getSource()->getIndexOptionText($optionId); + if ($this->getFieldType($attribute) == FieldInterface::FIELD_TYPE_BOOLEAN) { + $optionValue = $attribute->getStoreLabel($storeId); + } $this->attributeOptionTextCache[$storeId][$attributeId][$optionId] = $optionValue; } diff --git a/src/module-elasticsuite-catalog/Model/Layer/Filter/Attribute.php b/src/module-elasticsuite-catalog/Model/Layer/Filter/Attribute.php index a9e7eea4b..141a411ed 100644 --- a/src/module-elasticsuite-catalog/Model/Layer/Filter/Attribute.php +++ b/src/module-elasticsuite-catalog/Model/Layer/Filter/Attribute.php @@ -15,7 +15,6 @@ namespace Smile\ElasticSuiteCatalog\Model\Layer\Filter; use Smile\ElasticSuiteCore\Search\Request\BucketInterface; -use Magento\Catalog\Model\Layer\Filter\Item\DataBuilder; /** * Product attribute filter implementation. @@ -29,8 +28,7 @@ class Attribute extends \Magento\CatalogSearch\Model\Layer\Filter\Attribute /** * @var array */ - private $currentFilterValue = []; - + protected $currentFilterValue = []; /** * @var \Magento\Framework\Filter\StripTags @@ -171,7 +169,7 @@ protected function _initItems() * * @return string */ - private function getFilterField() + protected function getFilterField() { $field = $this->getAttributeModel()->getAttributeCode(); diff --git a/src/module-elasticsuite-catalog/Model/Layer/Filter/Boolean.php b/src/module-elasticsuite-catalog/Model/Layer/Filter/Boolean.php new file mode 100644 index 000000000..dbc13e28c --- /dev/null +++ b/src/module-elasticsuite-catalog/Model/Layer/Filter/Boolean.php @@ -0,0 +1,103 @@ + + * @copyright 2016 Smile + * @license Open Software License ("OSL") v. 3.0 + */ +namespace Smile\ElasticSuiteCatalog\Model\Layer\Filter; + +/** + * Product boolean filter implementation. + * + * @category Smile + * @package Smile_ElasticSuiteCatalog + * @author Romain Ruaud + */ +class Boolean extends Attribute +{ + /** + * {@inheritDoc} + */ + public function apply(\Magento\Framework\App\RequestInterface $request) + { + $attributeValue = $request->getParam($this->_requestVar); + + if (!empty($attributeValue)) { + if (!is_array($attributeValue)) { + $attributeValue = [$attributeValue]; + } + + $this->currentFilterValue = $attributeValue; + + /** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */ + $productCollection = $this->getLayer()->getProductCollection(); + + $productCollection->addFieldToFilter($this->getFilterField(), $attributeValue); + $layerState = $this->getLayer()->getState(); + + $booleanFilterLabels = []; + foreach ($this->currentFilterValue as $currentFilter) { + $booleanFilterLabels[] = (string) $this->getAttributeModel()->getSource()->getOptionText((int) $currentFilter); + } + $filterLabel = implode(', ', $booleanFilterLabels); + + $filter = $this->_createItem($filterLabel, $this->currentFilterValue); + + $layerState->addFilter($filter); + } + + return $this; + } + + /** + * Retrieve ES filter field. + * + * @return string + */ + protected function getFilterField() + { + $field = $this->getAttributeModel()->getAttributeCode(); + + return $field; + } + + /** + * @SuppressWarnings(PHPMD.CamelCaseMethodName) + * @SuppressWarnings(PHPMD.ElseExpression) + * + * {@inheritDoc} + */ + protected function _initItems() + { + parent::_initItems(); + + foreach ($this->_items as $item) { + $applyValue = $item->getLabel(); + + if ($item->getValue() == \Magento\Eav\Model\Entity\Attribute\Source\Boolean::VALUE_YES) { + if (is_numeric($item->getLabel())) { + $label = $this->getAttributeModel()->getSource()->getOptionText((int) $item->getLabel()); + $item->setLabel((string) $label); + } + } + + if (($valuePos = array_search($applyValue, $this->currentFilterValue)) !== false) { + $item->setIsSelected(true); + $applyValue = $this->currentFilterValue; + unset($applyValue[$valuePos]); + } else { + $applyValue = array_merge($this->currentFilterValue, [$applyValue]); + } + + $item->setApplyFilterValue(array_values($applyValue)); + } + + return $this; + } +} diff --git a/src/module-elasticsuite-catalog/Model/Layer/FilterList.php b/src/module-elasticsuite-catalog/Model/Layer/FilterList.php index 706dacb23..8956a56f0 100644 --- a/src/module-elasticsuite-catalog/Model/Layer/FilterList.php +++ b/src/module-elasticsuite-catalog/Model/Layer/FilterList.php @@ -22,6 +22,11 @@ */ class FilterList extends \Magento\Catalog\Model\Layer\FilterList { + /** + * Boolean filter name + */ + const BOOLEAN_FILTER = 'boolean'; + /** * {@inheritDoc} */ @@ -33,6 +38,12 @@ protected function getAttributeFilterClass(\Magento\Catalog\Model\ResourceModel\ $filterClassName = $this->filterTypes[self::DECIMAL_FILTER]; } + if (($attribute->getFrontendInput() == 'boolean') + && ($attribute->getSourceModel() == 'Magento\Eav\Model\Entity\Attribute\Source\Boolean') + && isset($this->filterTypes[self::BOOLEAN_FILTER])) { + $filterClassName = $this->filterTypes[self::BOOLEAN_FILTER]; + } + return $filterClassName; } } diff --git a/src/module-elasticsuite-catalog/etc/frontend/di.xml b/src/module-elasticsuite-catalog/etc/frontend/di.xml index 3d574db51..45c6fc7b2 100644 --- a/src/module-elasticsuite-catalog/etc/frontend/di.xml +++ b/src/module-elasticsuite-catalog/etc/frontend/di.xml @@ -23,6 +23,7 @@ Smile\ElasticSuiteCatalog\Model\Layer\Filter\Price Smile\ElasticSuiteCatalog\Model\Layer\Filter\Decimal categoryFilterUsingRewrite + Smile\ElasticSuiteCatalog\Model\Layer\Filter\Boolean @@ -35,6 +36,7 @@ Smile\ElasticSuiteCatalog\Model\Layer\Filter\Price Smile\ElasticSuiteCatalog\Model\Layer\Filter\Decimal Smile\ElasticSuiteCatalog\Model\Layer\Filter\Category + Smile\ElasticSuiteCatalog\Model\Layer\Filter\Boolean