Skip to content

Commit 8f9f248

Browse files
MC-16650: Product Attribute Type Price Not Displaying
- extract hard to read logic and add integration tests
1 parent a4f2c53 commit 8f9f248

File tree

5 files changed

+210
-1
lines changed

5 files changed

+210
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
7676
->getProductCollection()
7777
->addFieldToFilter(
7878
$this->getAttributeModel()->getAttributeCode(),
79-
['from' => $from, 'to' => empty($to) || $from === $to ? $to : $to - 0.001]
79+
['from' => $from, 'to' => $this->getToRangeValue($from, $to)]
8080
);
8181

8282
$this->getLayer()->getState()->addFilter(
@@ -149,4 +149,23 @@ protected function renderRangeLabel($fromPrice, $toPrice)
149149
return __('%1 - %2', $formattedFromPrice, $this->priceCurrency->format($toPrice));
150150
}
151151
}
152+
153+
/**
154+
* Get the to range value
155+
*
156+
* When the range is 10-20 we only need to get products that are in the 10-19.99 range.
157+
* 20 should be in the next range group.
158+
*
159+
* @param float|string $from
160+
* @param float|string $to
161+
* @return float|string
162+
*/
163+
private function getToRangeValue($from, $to)
164+
{
165+
if (!empty($to) && $from !== $to) {
166+
$to -= 0.001;
167+
}
168+
169+
return $to;
170+
}
152171
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $installer \Magento\Catalog\Setup\CategorySetup */
8+
$installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
9+
\Magento\Catalog\Setup\CategorySetup::class
10+
);
11+
12+
$installer->updateAttribute('catalog_product', 'special_price', 'is_filterable', 1);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $product \Magento\Catalog\Model\Product */
8+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
9+
$product->isObjectNew(true);
10+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
11+
->setId(10)
12+
->setAttributeSetId(4)
13+
->setName('Simple Product1')
14+
->setSku('simple1')
15+
->setTaxClassId('none')
16+
->setDescription('description')
17+
->setShortDescription('short description')
18+
->setOptionsContainer('container1')
19+
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
20+
->setPrice(15)
21+
->setWeight(10)
22+
->setMetaTitle('meta title')
23+
->setMetaKeyword('meta keyword')
24+
->setMetaDescription('meta description')
25+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
26+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
27+
->setWebsiteIds([1])
28+
->setCategoryIds([2])
29+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
30+
->setSpecialPrice('10')
31+
->save();
32+
33+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
34+
$product->isObjectNew(true);
35+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
36+
->setId(11)
37+
->setAttributeSetId(4)
38+
->setName('Simple Product2')
39+
->setSku('simple2')
40+
->setTaxClassId('none')
41+
->setDescription('description')
42+
->setShortDescription('short description')
43+
->setOptionsContainer('container1')
44+
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE)
45+
->setPrice(25)
46+
->setWeight(20)
47+
->setMetaTitle('meta title')
48+
->setMetaKeyword('meta keyword')
49+
->setMetaDescription('meta description')
50+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
51+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
52+
->setWebsiteIds([1])
53+
->setCategoryIds([2])
54+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
55+
->setSpecialPrice('20')
56+
->save();
57+
58+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
59+
$product->isObjectNew(true);
60+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
61+
->setId(12)
62+
->setAttributeSetId(4)
63+
->setName('Simple Product3')
64+
->setSku('simple3')
65+
->setTaxClassId('none')
66+
->setDescription('description')
67+
->setShortDescription('short description')
68+
->setPrice(35)
69+
->setWeight(30)
70+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
71+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
72+
->setWebsiteIds([1])
73+
->setCategoryIds([2])
74+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 140, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
75+
->setSpecialPrice('30')
76+
->save();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
9+
/** @var \Magento\Framework\Registry $registry */
10+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
11+
12+
$registry->unregister('isSecureArea');
13+
$registry->register('isSecureArea', true);
14+
15+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
16+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
17+
18+
foreach (['simple1', 'simple2', 'simple3'] as $sku) {
19+
try {
20+
$product = $productRepository->get($sku, false, null, true);
21+
$productRepository->delete($product);
22+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
23+
//Product already removed
24+
}
25+
}
26+
27+
$registry->unregister('isSecureArea');
28+
$registry->register('isSecureArea', false);

dev/tests/integration/testsuite/Magento/CatalogSearch/Model/Layer/Filter/DecimalTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,80 @@ protected function setUp()
4949
$this->_model->setAttributeModel($attribute);
5050
}
5151

52+
/**
53+
* Test the product collection returns the correct number of items after the filter is applied.
54+
*
55+
* @magentoDataFixture Magento/Catalog/Model/Layer/Filter/_files/attribute_special_price_filterable.php
56+
* @magentoDataFixture Magento/Catalog/_files/multiple_visible_products.php
57+
* @magentoDbIsolation disabled
58+
*/
59+
public function testApplyProductCollection()
60+
{
61+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
62+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
63+
$category = $objectManager->create(\Magento\Catalog\Model\Category::class);
64+
$category->load(2);
65+
$this->_model->getLayer()->setCurrentCategory($category);
66+
67+
/** @var $attribute \Magento\Catalog\Model\Entity\Attribute */
68+
$attribute = $objectManager->create(\Magento\Catalog\Model\Entity\Attribute::class);
69+
$attribute->loadByCode('catalog_product', 'special_price');
70+
$this->_model->setAttributeModel($attribute);
71+
72+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
73+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
74+
/** @var $request \Magento\TestFramework\Request */
75+
$request = $objectManager->get(\Magento\TestFramework\Request::class);
76+
$request->setParam('special_price', '10-20');
77+
$result = $this->_model->apply($request);
78+
$collection = $this->_model->getLayer()->getProductCollection();
79+
$size = $collection->getSize();
80+
$this->assertEquals(
81+
1,
82+
$size
83+
);
84+
}
85+
86+
/**
87+
* Test the filter label is correct
88+
*/
89+
public function testApplyFilterLabel()
90+
{
91+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
92+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
93+
/** @var $request \Magento\TestFramework\Request */
94+
$request = $objectManager->get(\Magento\TestFramework\Request::class);
95+
$request->setParam('weight', '10-20');
96+
$this->_model->apply($request);
97+
98+
$filters = $this->_model->getLayer()->getState()->getFilters();
99+
$this->assertArrayHasKey(0, $filters);
100+
$this->assertEquals(
101+
'<span class="price">$10.00</span> - <span class="price">$19.99</span>',
102+
(string)$filters[0]->getLabel()
103+
);
104+
}
105+
106+
/**
107+
* Test the filter label is correct when there is empty To value
108+
*/
109+
public function testApplyFilterLabelWithEmptyToValue()
110+
{
111+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
112+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
113+
/** @var $request \Magento\TestFramework\Request */
114+
$request = $objectManager->get(\Magento\TestFramework\Request::class);
115+
$request->setParam('weight', '10-');
116+
$this->_model->apply($request);
117+
118+
$filters = $this->_model->getLayer()->getState()->getFilters();
119+
$this->assertArrayHasKey(0, $filters);
120+
$this->assertEquals(
121+
'<span class="price">$10.00</span> and above',
122+
(string)$filters[0]->getLabel()
123+
);
124+
}
125+
52126
public function testApplyNothing()
53127
{
54128
$this->assertEmpty($this->_model->getData('range'));

0 commit comments

Comments
 (0)