Skip to content

7455/price slider #52

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

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
20 changes: 13 additions & 7 deletions main/src/Model/Bridge/AttributeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute as AttributeResource;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;

class AttributeRepository implements AttributeRepositoryInterface
{
Expand All @@ -28,18 +29,20 @@ class AttributeRepository implements AttributeRepositoryInterface
* @var AttributeSearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;

/**
* AttributeRepository constructor.
* @param ProductAttributeRepositoryInterface $attributeRepository
* @param AttributeSearchCriteriaBuilder $searchCriteriaBuilder
* @var StoreManagerInterface
*/
public function __construct(ProductAttributeRepositoryInterface $attributeRepository,
AttributeSearchCriteriaBuilder $searchCriteriaBuilder)
{
private $storeManager;

public function __construct(
ProductAttributeRepositoryInterface $attributeRepository,
AttributeSearchCriteriaBuilder $searchCriteriaBuilder,
StoreManagerInterface $storeManager
) {
$this->attributeRepository = $attributeRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder->except(['status']);
$this->attributeStorage = new \SplObjectStorage;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -124,6 +127,9 @@ public function getAttributeCodesToIndex()
*/
public function getAttributeByCode($attributeCode, $storeId)
{
if ($storeId === null) {
$storeId = $this->storeManager->getStore()->getId();
}
try {
$magentoAttribute = $this->attributeRepository->get($attributeCode);
} catch (NoSuchEntityException $e) {
Expand Down
137 changes: 137 additions & 0 deletions main/src/Model/Plugin/CatalogsearchFilterDecimalPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php
/**
* integer_net Magento Module
*
* @category IntegerNet
* @package IntegerNet
* @copyright Copyright (c) 2016 integer_net GmbH (http://www.integer-net.de/)
* @author Fabian Schmengler <[email protected]>
*/

namespace IntegerNet\Solr\Model\Plugin;

use Magento\Catalog\Model\Layer\Filter\ItemFactory as FilterItemFactory;
use Magento\CatalogSearch\Model\Layer\Filter\Decimal as Subject;
use Magento\Framework\App\RequestInterface;

/**
* Plugin to display multiple filters for same attribute in state block
*/
class CatalogsearchFilterDecimalPlugin
{
/**
* @var FilterItemFactory
*/
private $filterItemFactory;
/**
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
*/
private $priceCurrency;
/**
* @var \Magento\Catalog\Model\Layer\Filter\DataProvider\PriceFactory
*/
private $dataProviderFactory;

public function __construct(
FilterItemFactory $filterItemFactory,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
\Magento\Catalog\Model\Layer\Filter\DataProvider\PriceFactory $dataProviderFactory
) {
$this->filterItemFactory = $filterItemFactory;
$this->priceCurrency = $priceCurrency;
$this->dataProviderFactory = $dataProviderFactory;
}

public function aroundApply(Subject $subject, \Closure $proceed, RequestInterface $request)
{
$attributeCode = $subject->getRequestVar();
$attributeValue = $request->getParam($attributeCode);

if ((!$attributeValue) || !is_array($attributeValue)) {
return $proceed($request);
}

$fromParts = [];
$toParts = [];
foreach ($attributeValue as $attributeValueArrayPart) {
$filterParams = explode(',', $attributeValueArrayPart);
$dataProvider = $this->getDataProvider($subject);
$filter = $dataProvider->validateFilter($filterParams[0]);
if (!$filter) {
continue;
}

$dataProvider->setInterval($filter);
$priorFilters = $dataProvider->getPriorFilters($filterParams);
if ($priorFilters) {
$dataProvider->setPriorIntervals($priorFilters);
}

list($from, $to) = $filter;

$fromParts[] = $from;
$toParts[] = $to;

$subject->getLayer()->getState()->addFilter(
$this->_createItem($subject, $this->_renderRangeLabel($subject, empty($from) ? 0 : $from, $to), $filter)
);
}

/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
$productCollection = $subject->getLayer()
->getProductCollection();
$productCollection->addFieldToFilter(
$attributeCode,
['from' => $fromParts, 'to' => $toParts]
);


return $subject;
}

/**
* Create filter item object
*
* @param Subject $subject
* @param string $label
* @param mixed $value
* @param int $count
* @return \Magento\Catalog\Model\Layer\Filter\Item
*/
protected function _createItem(Subject $subject, $label, $value, $count = 0)
{
return $this->filterItemFactory->create()
->setFilter($subject)
->setLabel($label)
->setValue($value)
->setCount($count);
}

/**
* Prepare text of range label
*
* @param Subject $subject
* @param float|string $fromValue
* @param float|string $toValue
* @return float|\Magento\Framework\Phrase
*/
protected function _renderRangeLabel(Subject $subject, $fromValue, $toValue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with current Magento 2 standards it should be private and without underscore

{
if ($toValue === '') {
return __('%1 and above', $fromValue);
} elseif ($fromValue == $toValue && $this->getDataProvider($subject)->getOnePriceIntervalValue()) {
return $fromValue;
} else {
return __('%1 - %2', $fromValue, $toValue);
}
}

/**
* @param Subject $subject
* @return \Magento\Catalog\Model\Layer\Filter\DataProvider\Price
*/
private function getDataProvider(Subject $subject)
{
return $this->dataProviderFactory->create(['layer' => $subject->getLayer()]);
}
}
2 changes: 1 addition & 1 deletion main/src/Model/Plugin/CatalogsearchFilterPricePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function aroundApply(Subject $subject, \Closure $proceed, RequestInterfac
$productCollection = $subject->getLayer()
->getProductCollection();
$productCollection->addFieldToFilter(
'price',
$attributeCode,
['from' => $fromParts, 'to' => $toParts]
);

Expand Down
27 changes: 27 additions & 0 deletions main/src/Model/Search/Adapter/DecimalFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* integer_net Magento Module
*
* @category IntegerNet
* @package IntegerNet_Solr
* @copyright Copyright (c) 2017 integer_net GmbH (http://www.integer-net.de/)
* @author Andreas von Studnitz <[email protected]>
*/

namespace IntegerNet\Solr\Model\Search\Adapter;

class DecimalFilter extends \Magento\CatalogSearch\Model\Layer\Filter\Decimal
{

/**
* Checks whether the option reduces the number of results
*
* @param int $optionCount Count of search results with this option
* @param int $totalSize Current search results count
* @return bool
*/
protected function isOptionReducesResults($optionCount, $totalSize)
{
return true;
}
}
28 changes: 23 additions & 5 deletions main/src/Model/Search/Adapter/FilterConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace IntegerNet\Solr\Model\Search\Adapter;

use IntegerNet\Solr\Implementor\AttributeRepository;
use IntegerNet\Solr\Indexer\IndexField;
use IntegerNet\Solr\Model\Bridge\EventDispatcher;
use IntegerNet\Solr\Query\Params\FilterQueryBuilder;
use Magento\Framework\Search\Request as MagentoRequest;
use Psr\Log\LoggerInterface;
Expand All @@ -30,11 +32,19 @@ class FilterConverter
* @var LoggerInterface
*/
private $logger;
/**
* @var EventDispatcher
*/
private $eventDispatcher;

public function __construct(AttributeRepository $attributeRepository, LoggerInterface $logger)
{
public function __construct(
AttributeRepository $attributeRepository,
LoggerInterface $logger,
EventDispatcher $eventDispatcher
) {
$this->attributeRepository = $attributeRepository;
$this->logger = $logger;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -49,7 +59,7 @@ public function configure(FilterQueryBuilder $fqBuilder, MagentoRequest\Query\Fi
if ($reference instanceof MagentoRequest\Filter\Term) {
$this->configureTermFilter($fqBuilder, $reference, $storeId);
} elseif ($reference instanceof MagentoRequest\Filter\Range) {
$this->configureRangeFilter($fqBuilder, $reference);
$this->configureRangeFilter($fqBuilder, $reference, $storeId);
} else {
$this->log(sprintf('Unknown filter reference %s (type: %s)', get_class($reference), $filter->getReferenceType()));
}
Expand All @@ -71,15 +81,23 @@ private function configureTermFilter(FilterQueryBuilder $fqBuilder, MagentoReque
);
}
}
private function configureRangeFilter(FilterQueryBuilder $fqBuilder, MagentoRequest\Filter\Range $range)
private function configureRangeFilter(FilterQueryBuilder $fqBuilder, MagentoRequest\Filter\Range $range, $storeId)
{
if ($range->getField() === 'price') {
$fqBuilder->addPriceRangeFilterByMinMax(
$range->getFrom(),
$range->getTo()
);
} else {
$this->log(sprintf('Unknown range field %s', $range->getField()));
$indexField = new IndexField(
$this->attributeRepository->getAttributeByCode($range->getField(), $storeId),
$this->eventDispatcher
);
$fqBuilder->addRangeFilterByMinMax(
$indexField->getFieldName(),
$range->getFrom(),
$range->getTo()
);
}
}

Expand Down
Loading