Skip to content

Commit fab4b82

Browse files
author
Oleksii Korshenko
authored
ENGCOM-797: Fix for Issue-13556 - Sorting in Product Listing via Quantity not work #13691
2 parents 40aaa21 + 750dbf2 commit fab4b82

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

app/code/Magento/CatalogInventory/Model/Source/Stock.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,23 @@ public function getAllOptions()
2626
['value' => \Magento\CatalogInventory\Model\Stock::STOCK_OUT_OF_STOCK, 'label' => __('Out of Stock')]
2727
];
2828
}
29+
30+
/**
31+
* Add Value Sort To Collection Select.
32+
*
33+
* @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
34+
* @param string $dir
35+
*
36+
* @return $this
37+
*/
38+
public function addValueSortToCollection($collection, $dir = \Magento\Framework\Data\Collection::SORT_ORDER_DESC)
39+
{
40+
$collection->getSelect()->joinLeft(
41+
['stock_item_table' => 'cataloginventory_stock_item'],
42+
"e.entity_id=stock_item_table.product_id",
43+
[]
44+
);
45+
$collection->getSelect()->order("stock_item_table.qty $dir");
46+
return $this;
47+
}
2948
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogInventory\Test\Unit\Model\Source;
8+
9+
use PHPUnit\Framework\TestCase;
10+
11+
class StockTest extends TestCase
12+
{
13+
/**
14+
* @var \Magento\CatalogInventory\Model\Source\Stock
15+
*/
16+
private $model;
17+
18+
protected function setUp()
19+
{
20+
$this->model = new \Magento\CatalogInventory\Model\Source\Stock();
21+
}
22+
23+
public function testAddValueSortToCollection()
24+
{
25+
$selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
26+
$collectionMock = $this->createMock(\Magento\Eav\Model\Entity\Collection\AbstractCollection::class);
27+
$collectionMock->expects($this->atLeastOnce())->method('getSelect')->willReturn($selectMock);
28+
29+
$selectMock->expects($this->once())
30+
->method('joinLeft')
31+
->with(
32+
['stock_item_table' => 'cataloginventory_stock_item'],
33+
"e.entity_id=stock_item_table.product_id",
34+
[]
35+
)
36+
->willReturnSelf();
37+
$selectMock->expects($this->once())
38+
->method('order')
39+
->with("stock_item_table.qty DESC")
40+
->willReturnSelf();
41+
42+
$this->model->addValueSortToCollection($collectionMock);
43+
}
44+
}

0 commit comments

Comments
 (0)