init
This commit is contained in:
+152
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
/**
|
||||
* @category WeltPixel
|
||||
* @package WeltPixel_LayeredNavigation
|
||||
* @copyright Copyright (c) 2018 Weltpixel
|
||||
* @author Weltpixel TEAM
|
||||
*/
|
||||
|
||||
namespace WeltPixel\LayeredNavigation\Model\Layer;
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use WeltPixel\LayeredNavigation\Model\AttributeOptions;
|
||||
|
||||
/**
|
||||
* Class Filter
|
||||
* @package WeltPixel\LayeredNavigation\Model\Layer
|
||||
*/
|
||||
class Filter
|
||||
{
|
||||
/** @var \Magento\Framework\App\RequestInterface */
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* @var AttributeOptions
|
||||
*/
|
||||
protected $_wpAttributeOptions;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_ids = [];
|
||||
|
||||
/**
|
||||
* Filter constructor.
|
||||
* @param RequestInterface $request
|
||||
* @param AttributeOptions $attributeOptions
|
||||
*/
|
||||
public function __construct(
|
||||
RequestInterface $request,
|
||||
AttributeOptions $attributeOptions
|
||||
)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->_wpAttributeOptions = $attributeOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get option url. If it has been filtered, return removed url. Else return filter url
|
||||
*
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\Item $item
|
||||
* @return mixed
|
||||
*/
|
||||
public function getItemUrl($item)
|
||||
{
|
||||
if ($this->isSelected($item)) {
|
||||
return $item->getRemoveUrl();
|
||||
}
|
||||
|
||||
return $item->getUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter
|
||||
* @param bool|true $explode
|
||||
* @return array|mixed
|
||||
*/
|
||||
public function getFilterValue($filter, $explode = true)
|
||||
{
|
||||
$filterValue = $this->request->getParam($filter->getRequestVar());
|
||||
if (empty($filterValue)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $explode ? explode(',', $filterValue) : $filterValue;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether the option reduces the number of results
|
||||
*
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter
|
||||
* @param int $optionCount Count of search results with this option
|
||||
* @param int $totalSize Current search results count
|
||||
* @return bool
|
||||
*/
|
||||
public function isOptionReducesResults($filter, $optionCount, $totalSize)
|
||||
{
|
||||
$result = $optionCount <= $totalSize;
|
||||
|
||||
if ($this->isShowZero($filter)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $optionCount && $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter
|
||||
* @return bool
|
||||
*/
|
||||
public function isShowZero($filter)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $filter
|
||||
* @return bool
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function isMainFilter($filter) {
|
||||
if(empty($this->_ids)){
|
||||
$this->_ids = $this->getStateAttributesIds($filter);
|
||||
}
|
||||
$isMulti = false;
|
||||
if(count($this->_ids) <= 1) {
|
||||
$isMulti = true;
|
||||
if(!empty($this->_ids)) {
|
||||
$isMulti = $this->isMultiselect($this->_ids[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return $isMulti;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attrId
|
||||
*/
|
||||
public function isMultiselect($attrId) {
|
||||
$wpLnAttributeOptions = ($attrId) ? $this->_wpAttributeOptions->getDisplayOptionsByAttribute($attrId) : false;
|
||||
$isMultiselect = ($wpLnAttributeOptions) ? $wpLnAttributeOptions->getIsMultiselect() : false;
|
||||
|
||||
return $isMultiselect;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Layer $layer
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
protected function getStateAttributesIds($filter)
|
||||
{
|
||||
$layer = $filter->getLayer();
|
||||
foreach ($layer->getState()->getFilters() as $filter) {
|
||||
if ($model = $filter->getFilter()->getData('attribute_model')) {
|
||||
$this->_ids[] = $model->getId();
|
||||
}
|
||||
}
|
||||
return array_unique($this->_ids);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
<?php
|
||||
/**
|
||||
* @category WeltPixel
|
||||
* @package WeltPixel_LayeredNavigation
|
||||
* @copyright Copyright (c) 2018 Weltpixel
|
||||
* @author Weltpixel TEAM
|
||||
*/
|
||||
|
||||
namespace WeltPixel\LayeredNavigation\Model\Layer\Filter;
|
||||
|
||||
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
|
||||
use WeltPixel\LayeredNavigation\Helper\Data as LayerHelper;
|
||||
use WeltPixel\LayeredNavigation\Model\Layer\Filter as LayerFilter;
|
||||
use WeltPixel\LayeredNavigation\Model\AttributeOptions;
|
||||
|
||||
/**
|
||||
* Class Attribute
|
||||
* @package WeltPixel\LayeredNavigation\Model\Layer\Filter
|
||||
*/
|
||||
class Attribute extends AbstractFilter
|
||||
{
|
||||
/**
|
||||
* @var LayerHelper
|
||||
*/
|
||||
protected $_moduleHelper;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $_isFilter = true;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Filter\StripTags
|
||||
*/
|
||||
protected $tagFilter;
|
||||
|
||||
/**
|
||||
* @var LayerFilter
|
||||
*/
|
||||
protected $_layerFilter;
|
||||
|
||||
/**
|
||||
* @var AttributeOptions
|
||||
*/
|
||||
protected $_wpAttributeOptions;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $_requestParamVal;
|
||||
|
||||
/**
|
||||
* Resource instance
|
||||
*
|
||||
* @var \Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute
|
||||
*/
|
||||
protected $_resource;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $_originalCollection;
|
||||
|
||||
/**
|
||||
* Attribute constructor.
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
* @param \Magento\Catalog\Model\Layer $layer
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder
|
||||
* @param \Magento\Framework\Filter\StripTags $tagFilter
|
||||
* @param \Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory $filterAttributeFactory
|
||||
* @param LayerHelper $moduleHelper
|
||||
* @param LayerFilter $layerFilter
|
||||
* @param AttributeOptions $attributeOptions
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Magento\Catalog\Model\Layer $layer,
|
||||
\Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
|
||||
\Magento\Framework\Filter\StripTags $tagFilter,
|
||||
\Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory $filterAttributeFactory,
|
||||
LayerHelper $moduleHelper,
|
||||
LayerFilter $layerFilter,
|
||||
AttributeOptions $attributeOptions,
|
||||
array $data = []
|
||||
)
|
||||
{
|
||||
parent::__construct(
|
||||
$filterItemFactory,
|
||||
$storeManager,
|
||||
$layer,
|
||||
$itemDataBuilder,
|
||||
$data
|
||||
);
|
||||
$this->_resource = $filterAttributeFactory->create();
|
||||
$this->tagFilter = $tagFilter;
|
||||
$this->_moduleHelper = $moduleHelper;
|
||||
$this->_layerFilter = $layerFilter;
|
||||
$this->_wpAttributeOptions = $attributeOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function apply(\Magento\Framework\App\RequestInterface $request)
|
||||
{
|
||||
if (!$this->_moduleHelper->isEnabled()) {
|
||||
return parent::apply($request);
|
||||
}
|
||||
$attribute = $this->getAttributeModel();
|
||||
$attributeValue = $request->getParam($this->_requestVar);
|
||||
|
||||
if (empty($attributeValue)) {
|
||||
$this->_isFilter = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
$this->_requestParamVal = $this->_requestVar;
|
||||
$attributeValue = explode(',', $attributeValue);
|
||||
$state = $this->getLayer()->getState();
|
||||
foreach ($attributeValue as $value) {
|
||||
$label = $this->getOptionText($value);
|
||||
$state->addFilter($this->_createItem($label, $value));
|
||||
}
|
||||
|
||||
|
||||
if (!$this->isMultiselect($attribute->getId()) && count($attributeValue) > 1) {
|
||||
$attributeValue = array_slice($attributeValue, 0, 1);
|
||||
}
|
||||
|
||||
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
|
||||
$productCollection = $this->getLayer()->getProductCollection();
|
||||
if(!$this->_originalCollection) {
|
||||
$this->_originalCollection = $productCollection->getCollectionClone();
|
||||
}
|
||||
if (count($attributeValue) > 1) {
|
||||
if ($this->_moduleHelper->isElasticOrOpenSearchEngine()) {
|
||||
$productCollection->addFieldToFilter($attribute->getAttributeCode(), $attributeValue);
|
||||
} else {
|
||||
$productCollection->addFieldToFilter($attribute->getAttributeCode(), ['in' => $attributeValue]);
|
||||
}
|
||||
} else {
|
||||
$productCollection->addFieldToFilter($attribute->getAttributeCode(), $attributeValue[0]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve resource instance
|
||||
*
|
||||
* @return \Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute
|
||||
*/
|
||||
protected function _getResource()
|
||||
{
|
||||
return $this->_resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _getItemsData()
|
||||
{
|
||||
if (!$this->_moduleHelper->isEnabled()) {
|
||||
return parent::_getItemsData();
|
||||
}
|
||||
$logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
|
||||
$attribute = $this->getAttributeModel();
|
||||
$wpLnAttributeOptions = ($attribute->getId()) ? $this->_wpAttributeOptions->getDisplayOptionsByAttribute($attribute->getId()) : false;
|
||||
|
||||
if(!$wpLnAttributeOptions->getIsMultiselect() && $this->_isFilter && $attribute->getAttributeCode() != 'category_option') {
|
||||
return [];
|
||||
}
|
||||
|
||||
/** @var \WeltPixel\LayeredNavigation\Model\ResourceModel\Fulltext\Collection $productCollection */
|
||||
$productCollection = $this->getLayer()->getProductCollection();
|
||||
|
||||
if ($this->_isFilter && ( $this->_layerFilter->isMainFilter($this) || $wpLnAttributeOptions->getIsMultiselect())) {
|
||||
$productCollection = $productCollection->getCollectionClone()
|
||||
->removeAttributeSearch($attribute->getAttributeCode());
|
||||
}
|
||||
|
||||
$optionsFacetedData = $productCollection->getFacetedData($attribute->getAttributeCode());
|
||||
|
||||
if (count($optionsFacetedData) === 0
|
||||
&& $this->getAttributeIsFilterable($attribute) !== static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
|
||||
) {
|
||||
return $this->itemDataBuilder->build();
|
||||
}
|
||||
|
||||
$productSize = $productCollection->getSize();
|
||||
|
||||
$itemData = [];
|
||||
$checkCount = false;
|
||||
$options = $attribute->getFrontend()->getSelectOptions();
|
||||
$counter = false;
|
||||
|
||||
foreach ($options as $option) {
|
||||
$sorted = false;
|
||||
if (empty($option['value'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $option['value'];
|
||||
|
||||
if($counter) {
|
||||
$count = isset($counter[$value]) ? (int)$counter[$value] : 0;
|
||||
} else {
|
||||
$count = isset($optionsFacetedData[$value]['count']) ? (int)$optionsFacetedData[$value]['count'] : 0;
|
||||
}
|
||||
|
||||
// Check filter type
|
||||
if ($this->getAttributeIsFilterable($attribute) == static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS && (!$this->_layerFilter->isOptionReducesResults($this, $count, $productSize)) && $count == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($count > 0) {
|
||||
$checkCount = true;
|
||||
}
|
||||
|
||||
$itemData[] = [
|
||||
'label' => $this->tagFilter->filter($option['label']),
|
||||
'value' => $value,
|
||||
'count' => $count
|
||||
];
|
||||
}
|
||||
|
||||
if ($checkCount) {
|
||||
if ($wpLnAttributeOptions->getSortBy() == 2) {
|
||||
usort($itemData, [$this, '_compareAz']);
|
||||
$sorted = true;
|
||||
}
|
||||
foreach ($itemData as $item) {
|
||||
$this->itemDataBuilder->addItemData($item['label'], $item['value'], $item['count']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($wpLnAttributeOptions->getSortBy() == 2 && !$sorted) {
|
||||
usort($options, [$this, '_compareAz']);
|
||||
}
|
||||
return $this->itemDataBuilder->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* return attribute custom 'is_multiselect' option
|
||||
*
|
||||
* @param $attrId
|
||||
* @return bool
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
protected function isMultiselect($attrId)
|
||||
{
|
||||
$wpLnAttributeOptions = ($attrId) ? $this->_wpAttributeOptions->getDisplayOptionsByAttribute($attrId) : false;
|
||||
$isMultiselect = ($wpLnAttributeOptions) ? $wpLnAttributeOptions->getIsMultiselect() : false;
|
||||
|
||||
return $isMultiselect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $a
|
||||
* @param $b
|
||||
* @return int
|
||||
*/
|
||||
protected function _compareAz($a, $b)
|
||||
{
|
||||
return strcmp($a["label"], $b["label"]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
/**
|
||||
* @category WeltPixel
|
||||
* @package WeltPixel_LayeredNavigation
|
||||
* @copyright Copyright (c) 2018 Weltpixel
|
||||
* @author Weltpixel TEAM
|
||||
*/
|
||||
|
||||
namespace WeltPixel\LayeredNavigation\Model\Layer\Filter;
|
||||
|
||||
use Magento\CatalogSearch\Model\Layer\Filter\Category as AbstractFilter;
|
||||
use WeltPixel\LayeredNavigation\Helper\Data as LayerHelper;
|
||||
use WeltPixel\LayeredNavigation\Model\Layer\Filter as LayerFilter;
|
||||
|
||||
/**
|
||||
* Class Category
|
||||
* @package WeltPixel\LayeredNavigation\Model\Layer\Filter
|
||||
*/
|
||||
class Category extends AbstractFilter
|
||||
{
|
||||
/** @var \WeltPixel\LayeredNavigation\Helper\Data */
|
||||
protected $_moduleHelper;
|
||||
|
||||
/** @var bool Is Filterable Flag */
|
||||
protected $_isFilter = false;
|
||||
|
||||
/** @var \Magento\Framework\Escaper */
|
||||
private $escaper;
|
||||
|
||||
/** @var \Magento\Catalog\Model\Layer\Filter\DataProvider\Category */
|
||||
private $dataProvider;
|
||||
|
||||
/**
|
||||
* Category constructor.
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
* @param \Magento\Catalog\Model\Layer $layer
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder
|
||||
* @param \Magento\Framework\Escaper $escaper
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\DataProvider\CategoryFactory $categoryDataProviderFactory
|
||||
* @param LayerHelper $moduleHelper
|
||||
* @param LayerFilter $layerFilter
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Magento\Catalog\Model\Layer $layer,
|
||||
\Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
|
||||
\Magento\Framework\Escaper $escaper,
|
||||
\Magento\Catalog\Model\Layer\Filter\DataProvider\CategoryFactory $categoryDataProviderFactory,
|
||||
LayerHelper $moduleHelper,
|
||||
LayerFilter $layerFilter,
|
||||
array $data = []
|
||||
)
|
||||
{
|
||||
parent::__construct(
|
||||
$filterItemFactory,
|
||||
$storeManager,
|
||||
$layer,
|
||||
$itemDataBuilder,
|
||||
$escaper,
|
||||
$categoryDataProviderFactory,
|
||||
$data
|
||||
);
|
||||
|
||||
$this->escaper = $escaper;
|
||||
$this->_moduleHelper = $moduleHelper;
|
||||
$this->_layerFilter = $layerFilter;
|
||||
$this->dataProvider = $categoryDataProviderFactory->create(['layer' => $this->getLayer()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function apply(\Magento\Framework\App\RequestInterface $request)
|
||||
{
|
||||
if (!$this->_moduleHelper->isEnabled()) {
|
||||
return parent::apply($request);
|
||||
}
|
||||
|
||||
$categoryId = $request->getParam($this->_requestVar);
|
||||
if (empty($categoryId)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$categoryIds = [];
|
||||
foreach (explode(',', $categoryId) as $key => $id) {
|
||||
$this->dataProvider->setCategoryId($id);
|
||||
if ($this->dataProvider->isValid()) {
|
||||
$category = $this->dataProvider->getCategory();
|
||||
if ($request->getParam('id') != $id) {
|
||||
$categoryIds[] = $id;
|
||||
$this->getLayer()->getState()->addFilter($this->_createItem($category->getName(), $id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sizeof($categoryIds)) {
|
||||
$this->_isFilter = true;
|
||||
$this->getLayer()->getProductCollection()->addLayerCategoryFilter($categoryIds);
|
||||
}
|
||||
|
||||
if ($parentCategoryId = $request->getParam('id')) {
|
||||
$this->dataProvider->setCategoryId($parentCategoryId);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\StateException
|
||||
*/
|
||||
protected function _getItemsData()
|
||||
{
|
||||
if (!$this->_moduleHelper->isEnabled()) {
|
||||
return parent::_getItemsData();
|
||||
}
|
||||
|
||||
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
|
||||
$productCollection = $this->getLayer()->getProductCollection();
|
||||
|
||||
if ($this->_isFilter) {
|
||||
$productCollection = $productCollection->getCollectionClone()
|
||||
->removeAttributeSearch('category_ids');
|
||||
}
|
||||
|
||||
$optionsFacetedData = $productCollection->getFacetedData('category');
|
||||
$category = $this->dataProvider->getCategory();
|
||||
$categories = $category->getChildrenCategories();
|
||||
|
||||
$collectionSize = $productCollection->getSize();
|
||||
|
||||
if ($category->getIsActive()) {
|
||||
foreach ($categories as $category) {
|
||||
$count = isset($optionsFacetedData[$category->getId()]) ? $optionsFacetedData[$category->getId()]['count'] : 0;
|
||||
if ($category->getIsActive()
|
||||
&& $this->_layerFilter->isOptionReducesResults($this, $count, $collectionSize)
|
||||
) {
|
||||
$this->itemDataBuilder->addItemData(
|
||||
$this->escaper->escapeHtml($category->getName()),
|
||||
$category->getId(),
|
||||
$count
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->itemDataBuilder->build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
/**
|
||||
* @category WeltPixel
|
||||
* @package WeltPixel_LayeredNavigation
|
||||
* @copyright Copyright (c) 2018 Weltpixel
|
||||
* @author Weltpixel TEAM
|
||||
*/
|
||||
|
||||
namespace WeltPixel\LayerdNavigation\Model\Layer\Filter;
|
||||
|
||||
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
|
||||
|
||||
/**
|
||||
* Class Decimal
|
||||
* @package WeltPixel\LayerdNavigation\Model\Layer\Filter
|
||||
*/
|
||||
class Decimal extends AbstractFilter
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
|
||||
*/
|
||||
private $priceCurrency;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\ResourceModel\Layer\Filter\Decimal
|
||||
*/
|
||||
private $resource;
|
||||
|
||||
/**
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
* @param \Magento\Catalog\Model\Layer $layer
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder
|
||||
* @param \Magento\Catalog\Model\ResourceModel\Layer\Filter\DecimalFactory $filterDecimalFactory
|
||||
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Magento\Catalog\Model\Layer $layer,
|
||||
\Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
|
||||
\Magento\Catalog\Model\ResourceModel\Layer\Filter\DecimalFactory $filterDecimalFactory,
|
||||
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct(
|
||||
$filterItemFactory,
|
||||
$storeManager,
|
||||
$layer,
|
||||
$itemDataBuilder,
|
||||
$data
|
||||
);
|
||||
$this->resource = $filterDecimalFactory->create();
|
||||
$this->priceCurrency = $priceCurrency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply price range filter
|
||||
*
|
||||
* @param \Magento\Framework\App\RequestInterface $request
|
||||
* @return $this
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function apply(\Magento\Framework\App\RequestInterface $request)
|
||||
{
|
||||
/**
|
||||
* Filter must be string: $fromPrice-$toPrice
|
||||
*/
|
||||
$filter = $request->getParam($this->getRequestVar());
|
||||
if (!$filter || is_array($filter)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
list($from, $to) = explode('-', $filter);
|
||||
|
||||
$this->getLayer()
|
||||
->getProductCollection()
|
||||
->addFieldToFilter(
|
||||
$this->getAttributeModel()->getAttributeCode(),
|
||||
['from' => $from, 'to' => $to]
|
||||
);
|
||||
|
||||
$this->getLayer()->getState()->addFilter(
|
||||
$this->_createItem($this->renderRangeLabel(empty($from) ? 0 : $from, $to), $filter)
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data array for building attribute filter items
|
||||
*
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
*/
|
||||
protected function _getItemsData()
|
||||
{
|
||||
$attribute = $this->getAttributeModel();
|
||||
|
||||
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
|
||||
$productCollection = $this->getLayer()->getProductCollection();
|
||||
$productSize = $productCollection->getSize();
|
||||
$facets = $productCollection->getFacetedData($attribute->getAttributeCode());
|
||||
|
||||
$data = [];
|
||||
foreach ($facets as $key => $aggregation) {
|
||||
$count = $aggregation['count'];
|
||||
if (!$this->isOptionReducesResults($count, $productSize)) {
|
||||
continue;
|
||||
}
|
||||
list($from, $to) = explode('_', $key);
|
||||
if ($from == '*') {
|
||||
$from = '';
|
||||
}
|
||||
if ($to == '*') {
|
||||
$to = '';
|
||||
}
|
||||
$label = $this->renderRangeLabel(
|
||||
empty($from) ? 0 : $from,
|
||||
empty($to) ? $to : $to
|
||||
);
|
||||
$value = $from . '-' . $to;
|
||||
|
||||
$data[] = [
|
||||
'label' => $label,
|
||||
'value' => $value,
|
||||
'count' => $count,
|
||||
'from' => $from,
|
||||
'to' => $to
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare text of range label
|
||||
*
|
||||
* @param float|string $fromPrice
|
||||
* @param float|string $toPrice
|
||||
* @return \Magento\Framework\Phrase
|
||||
*/
|
||||
protected function renderRangeLabel($fromPrice, $toPrice)
|
||||
{
|
||||
$formattedFromPrice = $this->priceCurrency->format($fromPrice);
|
||||
if ($toPrice === '') {
|
||||
return __('%1 and above', $formattedFromPrice);
|
||||
} else {
|
||||
if ($fromPrice != $toPrice) {
|
||||
$toPrice -= .01;
|
||||
}
|
||||
return __('%1 - %2', $formattedFromPrice, $this->priceCurrency->format($toPrice));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
/**
|
||||
* @category WeltPixel
|
||||
* @package WeltPixel_LayeredNavigation
|
||||
* @copyright Copyright (c) 2018 Weltpixel
|
||||
* @author Weltpixel TEAM
|
||||
*/
|
||||
|
||||
namespace WeltPixel\LayeredNavigation\Model\Layer\Filter;
|
||||
|
||||
use Magento\CatalogSearch\Model\Layer\Filter\Price as AbstractFilter;
|
||||
use WeltPixel\LayeredNavigation\Helper\Data as LayerHelper;
|
||||
|
||||
/**
|
||||
* Class Price
|
||||
* @package WeltPixel\LayeredNavigation\Model\Layer\Filter
|
||||
*/
|
||||
class Price extends AbstractFilter
|
||||
{
|
||||
/** @var \WeltPixel\LayeredNavigation\Helper\Data */
|
||||
protected $_moduleHelper;
|
||||
|
||||
/** @var array|null Filter value */
|
||||
protected $_filterVal = null;
|
||||
|
||||
/** @var \Magento\Tax\Helper\Data */
|
||||
protected $_taxHelper;
|
||||
|
||||
/** @var \Magento\Catalog\Model\Layer\Filter\DataProvider\Price */
|
||||
private $dataProvider;
|
||||
|
||||
/** @var \Magento\Framework\Pricing\PriceCurrencyInterface */
|
||||
private $priceCurrency;
|
||||
|
||||
/**
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
* @param \Magento\Catalog\Model\Layer $layer
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder
|
||||
* @param \Magento\Catalog\Model\ResourceModel\Layer\Filter\Price $resource
|
||||
* @param \Magento\Customer\Model\Session $customerSession
|
||||
* @param \Magento\Framework\Search\Dynamic\Algorithm $priceAlgorithm
|
||||
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory $algorithmFactory
|
||||
* @param \Magento\Catalog\Model\Layer\Filter\DataProvider\PriceFactory $dataProviderFactory
|
||||
* @param \Magento\Tax\Helper\Data $taxHelper
|
||||
* @param \WeltPixel\LayeredNavigation\Helper\Data $moduleHelper
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Magento\Catalog\Model\Layer $layer,
|
||||
\Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
|
||||
\Magento\Catalog\Model\ResourceModel\Layer\Filter\Price $resource,
|
||||
\Magento\Customer\Model\Session $customerSession,
|
||||
\Magento\Framework\Search\Dynamic\Algorithm $priceAlgorithm,
|
||||
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
|
||||
\Magento\Catalog\Model\Layer\Filter\Dynamic\AlgorithmFactory $algorithmFactory,
|
||||
\Magento\Catalog\Model\Layer\Filter\DataProvider\PriceFactory $dataProviderFactory,
|
||||
\Magento\Tax\Helper\Data $taxHelper,
|
||||
LayerHelper $moduleHelper,
|
||||
array $data = []
|
||||
)
|
||||
{
|
||||
parent::__construct(
|
||||
$filterItemFactory,
|
||||
$storeManager,
|
||||
$layer,
|
||||
$itemDataBuilder,
|
||||
$resource,
|
||||
$customerSession,
|
||||
$priceAlgorithm,
|
||||
$priceCurrency,
|
||||
$algorithmFactory,
|
||||
$dataProviderFactory,
|
||||
$data
|
||||
);
|
||||
|
||||
$this->priceCurrency = $priceCurrency;
|
||||
$this->dataProvider = $dataProviderFactory->create(['layer' => $this->getLayer()]);
|
||||
$this->_moduleHelper = $moduleHelper;
|
||||
$this->_taxHelper = $taxHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function apply(\Magento\Framework\App\RequestInterface $request)
|
||||
{
|
||||
if (!$this->_moduleHelper->isEnabled()) {
|
||||
return parent::apply($request);
|
||||
}
|
||||
/**
|
||||
* Filter must be string: $fromPrice-$toPrice
|
||||
*/
|
||||
$filter = $request->getParam($this->getRequestVar());
|
||||
if (!$filter || is_array($filter)) {
|
||||
return $this;
|
||||
}
|
||||
$filterParams = explode(',', $filter);
|
||||
$filter = $this->dataProvider->validateFilter($filterParams[0]);
|
||||
if (!$filter) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->dataProvider->setInterval($filter);
|
||||
$priorFilters = $this->dataProvider->getPriorFilters($filterParams);
|
||||
if ($priorFilters) {
|
||||
$this->dataProvider->setPriorIntervals($priorFilters);
|
||||
}
|
||||
|
||||
list($from, $to) = $this->_filterVal = $filter;
|
||||
|
||||
$this->getLayer()->getProductCollection()->addFieldToFilter(
|
||||
'price',
|
||||
['from' => $from, 'to' => $to]
|
||||
);
|
||||
|
||||
$this->getLayer()->getState()->addFilter(
|
||||
$this->_createItem($this->_renderRangeLabel(empty($from) ? 0 : $from, $to), $filter)
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _renderRangeLabel($fromPrice, $toPrice, $isLast = false)
|
||||
{
|
||||
if (!$this->_moduleHelper->isEnabled()) {
|
||||
return parent::_renderRangeLabel($fromPrice, $toPrice, $isLast = false);
|
||||
}
|
||||
$formattedFromPrice = $this->priceCurrency->format($fromPrice);
|
||||
if ($toPrice === '' || $isLast) {
|
||||
return __('%1 and above', $formattedFromPrice);
|
||||
} elseif ($fromPrice == $toPrice && $this->dataProvider->getOnePriceIntervalValue()) {
|
||||
return $formattedFromPrice;
|
||||
} else {
|
||||
return __('%1 - %2', $formattedFromPrice, $this->priceCurrency->format($toPrice));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Price Slider Configuration
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSliderConfig()
|
||||
{
|
||||
/** @var \WeltPixel\LayeredNavigation\Model\ResourceModel\Fulltext\Collection $productCollection */
|
||||
$productCollection = $this->getLayer()->getProductCollection();
|
||||
|
||||
if ($this->_filterVal) {
|
||||
/** @type \WeltPixel\LayeredNavigation\Model\ResourceModel\Fulltext\Collection $productCollectionClone */
|
||||
$productCollection = $productCollection->getCollectionClone()
|
||||
->removeAttributeSearch(['price.from', 'price.to']);
|
||||
}
|
||||
|
||||
$min = $productCollection->getMinPrice();
|
||||
$max = $productCollection->getMaxPrice();
|
||||
|
||||
list($from, $to) = $this->_filterVal ?: [$min, $max];
|
||||
$from = ($from < $min) ? $min : $from;
|
||||
$to = ($to > $max) ? $max : $to;
|
||||
|
||||
$item = $this->getItems()[0];
|
||||
|
||||
return [
|
||||
"selectedFrom" => $from,
|
||||
"selectedTo" => $to,
|
||||
"minValue" => $min,
|
||||
"maxValue" => $max,
|
||||
"priceFormat" => $this->_taxHelper->getPriceFormat(),
|
||||
"ajaxUrl" => $item->getUrl()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data array for building attribute filter items
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
*/
|
||||
protected function _getItemsData()
|
||||
{
|
||||
if (!$this->_moduleHelper->isEnabled()) {
|
||||
return parent::_getItemsData();
|
||||
}
|
||||
|
||||
$attribute = $this->getAttributeModel();
|
||||
$this->_requestVar = $attribute->getAttributeCode();
|
||||
|
||||
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
|
||||
$productCollection = $this->getLayer()->getProductCollection();
|
||||
|
||||
if ($this->_filterVal) {
|
||||
/** @type \WeltPixel\LayeredNavigation\Model\ResourceModel\Fulltext\Collection $productCollectionClone */
|
||||
$productCollection = $productCollection->getCollectionClone()
|
||||
->removeAttributeSearch(['price.from', 'price.to']);
|
||||
}
|
||||
|
||||
$facets = $productCollection->getFacetedData($attribute->getAttributeCode());
|
||||
|
||||
$data = [];
|
||||
if (count($facets) > 1) {
|
||||
$lastFacet = array_key_last($facets);
|
||||
foreach ($facets as $key => $aggregation) {
|
||||
$count = $aggregation['count'];
|
||||
if (strpos($key, '_') === false) {
|
||||
continue;
|
||||
}
|
||||
$isLast = $lastFacet === $key;
|
||||
$data[] = $this->prepareData($key, $count, $isLast);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param int $count
|
||||
* @param boolean $isLast
|
||||
* @return array
|
||||
*/
|
||||
private function prepareData($key, $count, $isLast = false)
|
||||
{
|
||||
list($from, $to) = explode('_', $key);
|
||||
if ($from == '*') {
|
||||
$from = $this->getFrom($to);
|
||||
}
|
||||
if ($to == '*') {
|
||||
$to = $this->getTo($to);
|
||||
}
|
||||
$label = $this->_renderRangeLabel(
|
||||
empty($from) ? 0 : $from * $this->getCurrencyRate(),
|
||||
empty($to) ? $to : $to * $this->getCurrencyRate(),
|
||||
$isLast
|
||||
);
|
||||
$value = $from . '-' . $to . $this->dataProvider->getAdditionalRequestData();
|
||||
|
||||
$data = [
|
||||
'label' => $label,
|
||||
'value' => $value,
|
||||
'count' => $count,
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user