Skip to content

Commit bfce54a

Browse files
committed
Merge remote-tracking branch 'origin/MC-24217' into 2.4-develop-pr111
2 parents 7bb718e + 6844975 commit bfce54a

File tree

5 files changed

+242
-133
lines changed

5 files changed

+242
-133
lines changed

app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
namespace Magento\CatalogSearch\Model\Layer\Filter;
99

10+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1011
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
12+
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;
13+
use Magento\Framework\App\RequestInterface;
1114

1215
/**
1316
* Layer attribute filter
@@ -48,25 +51,27 @@ public function __construct(
4851
/**
4952
* Apply attribute option filter to product collection
5053
*
51-
* @param \Magento\Framework\App\RequestInterface $request
54+
* @param RequestInterface $request
5255
* @return $this
53-
* @throws \Magento\Framework\Exception\LocalizedException
5456
*/
55-
public function apply(\Magento\Framework\App\RequestInterface $request)
57+
public function apply(RequestInterface $request)
5658
{
5759
$attributeValue = $request->getParam($this->_requestVar);
5860
if (empty($attributeValue) && !is_numeric($attributeValue)) {
5961
return $this;
6062
}
6163

6264
$attribute = $this->getAttributeModel();
63-
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
65+
/** @var Collection $productCollection */
6466
$productCollection = $this->getLayer()
6567
->getProductCollection();
66-
$productCollection->addFieldToFilter($attribute->getAttributeCode(), $attributeValue);
68+
$productCollection->addFieldToFilter(
69+
$attribute->getAttributeCode(),
70+
$this->convertAttributeValue($attribute, $attributeValue)
71+
);
6772

6873
$labels = [];
69-
foreach ((array) $attributeValue as $value) {
74+
foreach ((array)$attributeValue as $value) {
7075
$label = $this->getOptionText($value);
7176
$labels[] = is_array($label) ? $label : [$label];
7277
}
@@ -76,6 +81,7 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
7681
->addFilter($this->_createItem($label, $attributeValue));
7782

7883
$this->setItems([]); // set items to disable show filtering
84+
7985
return $this;
8086
}
8187

@@ -88,7 +94,7 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
8894
protected function _getItemsData()
8995
{
9096
$attribute = $this->getAttributeModel();
91-
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
97+
/** @var Collection $productCollection */
9298
$productCollection = $this->getLayer()
9399
->getProductCollection();
94100
$optionsFacetedData = $productCollection->getFacetedData($attribute->getAttributeCode());
@@ -163,6 +169,22 @@ private function getOptionCount($value, $optionsFacetedData)
163169
: 0;
164170
}
165171

172+
/**
173+
* Convert attribute value according to its backend type.
174+
*
175+
* @param ProductAttributeInterface $attribute
176+
* @param mixed $value
177+
* @return int|string
178+
*/
179+
private function convertAttributeValue(ProductAttributeInterface $attribute, $value)
180+
{
181+
if ($attribute->getBackendType() === 'int') {
182+
return (int)$value;
183+
}
184+
185+
return $value;
186+
}
187+
166188
/**
167189
* @inheritdoc
168190
*/

app/code/Magento/CatalogSearch/Model/Layer/Filter/Decimal.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,49 @@
77

88
namespace Magento\CatalogSearch\Model\Layer\Filter;
99

10+
use Magento\Catalog\Model\Layer;
1011
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
12+
use Magento\Catalog\Model\Layer\Filter\Item\DataBuilder;
13+
use Magento\Catalog\Model\Layer\Filter\ItemFactory;
14+
use Magento\Catalog\Model\ResourceModel\Layer\Filter\Decimal as ResourceDecimal;
15+
use Magento\Catalog\Model\ResourceModel\Layer\Filter\DecimalFactory;
16+
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection as ProductCollection;
17+
use Magento\Framework\App\RequestInterface;
18+
use Magento\Framework\Phrase;
19+
use Magento\Framework\Pricing\PriceCurrencyInterface;
20+
use Magento\Store\Model\StoreManagerInterface;
1121

1222
/**
1323
* Layer decimal filter
1424
*/
1525
class Decimal extends AbstractFilter
1626
{
1727
/**
18-
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
28+
* @var PriceCurrencyInterface
1929
*/
2030
private $priceCurrency;
2131

2232
/**
23-
* @var \Magento\Catalog\Model\ResourceModel\Layer\Filter\Decimal
33+
* @var ResourceDecimal
2434
*/
2535
private $resource;
2636

2737
/**
28-
* @param \Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory
29-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
30-
* @param \Magento\Catalog\Model\Layer $layer
31-
* @param \Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder
32-
* @param \Magento\Catalog\Model\ResourceModel\Layer\Filter\DecimalFactory $filterDecimalFactory
33-
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
38+
* @param ItemFactory $filterItemFactory
39+
* @param StoreManagerInterface $storeManager
40+
* @param Layer $layer
41+
* @param DataBuilder $itemDataBuilder
42+
* @param DecimalFactory $filterDecimalFactory
43+
* @param PriceCurrencyInterface $priceCurrency
3444
* @param array $data
3545
*/
3646
public function __construct(
37-
\Magento\Catalog\Model\Layer\Filter\ItemFactory $filterItemFactory,
38-
\Magento\Store\Model\StoreManagerInterface $storeManager,
39-
\Magento\Catalog\Model\Layer $layer,
40-
\Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
41-
\Magento\Catalog\Model\ResourceModel\Layer\Filter\DecimalFactory $filterDecimalFactory,
42-
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
47+
ItemFactory $filterItemFactory,
48+
StoreManagerInterface $storeManager,
49+
Layer $layer,
50+
DataBuilder $itemDataBuilder,
51+
DecimalFactory $filterDecimalFactory,
52+
PriceCurrencyInterface $priceCurrency,
4353
array $data = []
4454
) {
4555
parent::__construct(
@@ -56,11 +66,10 @@ public function __construct(
5666
/**
5767
* Apply price range filter
5868
*
59-
* @param \Magento\Framework\App\RequestInterface $request
69+
* @param RequestInterface $request
6070
* @return $this
61-
* @throws \Magento\Framework\Exception\LocalizedException
6271
*/
63-
public function apply(\Magento\Framework\App\RequestInterface $request)
72+
public function apply(RequestInterface $request)
6473
{
6574
/**
6675
* Filter must be string: $fromPrice-$toPrice
@@ -71,6 +80,8 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
7180
}
7281

7382
list($from, $to) = explode('-', $filter);
83+
$from = (float)$from;
84+
$to = (float)$to;
7485

7586
$this->getLayer()
7687
->getProductCollection()
@@ -90,14 +101,12 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
90101
* Get data array for building attribute filter items
91102
*
92103
* @return array
93-
* @throws \Magento\Framework\Exception\LocalizedException
94-
* @SuppressWarnings(PHPMD.NPathComplexity)
95104
*/
96105
protected function _getItemsData()
97106
{
98107
$attribute = $this->getAttributeModel();
99108

100-
/** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
109+
/** @var ProductCollection $productCollection */
101110
$productCollection = $this->getLayer()->getProductCollection();
102111
$productSize = $productCollection->getSize();
103112
$facets = $productCollection->getFacetedData($attribute->getAttributeCode());
@@ -123,7 +132,7 @@ protected function _getItemsData()
123132
'value' => $value,
124133
'count' => $count,
125134
'from' => $from,
126-
'to' => $to
135+
'to' => $to,
127136
];
128137
}
129138

@@ -135,7 +144,7 @@ protected function _getItemsData()
135144
*
136145
* @param float|string $fromPrice
137146
* @param float|string $toPrice
138-
* @return \Magento\Framework\Phrase
147+
* @return Phrase
139148
*/
140149
protected function renderRangeLabel($fromPrice, $toPrice)
141150
{
@@ -146,6 +155,7 @@ protected function renderRangeLabel($fromPrice, $toPrice)
146155
if ($fromPrice != $toPrice) {
147156
$toPrice -= .01;
148157
}
158+
149159
return __('%1 - %2', $formattedFromPrice, $this->priceCurrency->format($toPrice));
150160
}
151161
}

0 commit comments

Comments
 (0)