File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed
app/code/Magento/CatalogInventory Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -26,4 +26,23 @@ public function getAllOptions()
26
26
['value ' => \Magento \CatalogInventory \Model \Stock::STOCK_OUT_OF_STOCK , 'label ' => __ ('Out of Stock ' )]
27
27
];
28
28
}
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
+ }
29
48
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments