Skip to content

Commit 6267566

Browse files
committed
#7768: Adding 'is_saleable' attribute to sort of product collection causes exception and adding 'is_salable' has no effect.
1 parent 3c59bd5 commit 6267566

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
16701670

16711671
return $this;
16721672
} elseif ($attribute == 'is_saleable') {
1673-
$this->getSelect()->order("is_saleable " . $dir);
1673+
$this->getSelect()->order("is_salable " . $dir);
16741674
return $this;
16751675
}
16761676

dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Product/CollectionTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,40 @@ public function testGetProductsWithTierPrice()
124124
$this->assertEquals(50, $tierPrices[2]->getExtensionAttributes()->getPercentageValue());
125125
$this->assertEquals(5, $tierPrices[2]->getValue());
126126
}
127+
128+
/**
129+
* Test addAttributeToSort() with attribute 'is_saleable' works properly on frontend.
130+
*
131+
* @dataProvider addAttributeToSortDataProvider
132+
* @magentoDataFixture Magento/Catalog/_files/multiple_products_with_non_saleable_product.php
133+
* @magentoConfigFixture current_store cataloginventory/options/show_out_of_stock 1
134+
* @magentoAppIsolation enabled
135+
* @magentoAppArea frontend
136+
*/
137+
public function testAddAttributeToSort(string $productSku, string $order)
138+
{
139+
/** @var Collection $productCollection */
140+
$this->collection->addAttributeToSort('is_saleable', $order);
141+
self::assertEquals(2, $this->collection->count());
142+
self::assertSame($productSku, $this->collection->getFirstItem()->getSku());
143+
}
144+
145+
/**
146+
* Provide test data for testAddAttributeToSort().
147+
*
148+
* @return array
149+
*/
150+
public function addAttributeToSortDataProvider()
151+
{
152+
return [
153+
[
154+
'product_sku' => 'simple_saleable',
155+
'order' => Collection::SORT_ORDER_DESC,
156+
],
157+
[
158+
'product_sku' => 'simple_not_saleable',
159+
'order' => Collection::SORT_ORDER_ASC,
160+
]
161+
];
162+
}
127163
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
->setAttributeSetId(4)
12+
->setName('Simple Product')
13+
->setSku('simple_saleable')
14+
->setTaxClassId('none')
15+
->setDescription('description')
16+
->setShortDescription('short description')
17+
->setOptionsContainer('container1')
18+
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
19+
->setPrice(10)
20+
->setWeight(1)
21+
->setMetaTitle('meta title')
22+
->setMetaKeyword('meta keyword')
23+
->setMetaDescription('meta description')
24+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
25+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
26+
->setWebsiteIds([1])
27+
->setCateroryIds([])
28+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
29+
->save();
30+
31+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
32+
$product->isObjectNew(true);
33+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
34+
->setAttributeSetId(4)
35+
->setName('Simple Product2')
36+
->setSku('simple_not_saleable')
37+
->setTaxClassId('none')
38+
->setDescription('description')
39+
->setShortDescription('short description')
40+
->setOptionsContainer('container1')
41+
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE)
42+
->setPrice(20)
43+
->setWeight(1)
44+
->setMetaTitle('meta title')
45+
->setMetaKeyword('meta keyword')
46+
->setMetaDescription('meta description')
47+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_CATALOG)
48+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
49+
->setWebsiteIds([1])
50+
->setCateroryIds([])
51+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 0])
52+
->save();
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 (['simple_saleable', 'simple_not_saleable'] 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);

0 commit comments

Comments
 (0)