5
5
*/
6
6
namespace Magento \CatalogInventory \Model \Source ;
7
7
8
+ use Magento \Catalog \Api \Data \ProductInterface ;
9
+ use Magento \CatalogInventory \Model \Stock as StockModel ;
8
10
use Magento \Eav \Model \Entity \Attribute \Source \AbstractSource ;
11
+ use Magento \Eav \Model \Entity \Collection \AbstractCollection ;
12
+ use Magento \Framework \Data \Collection ;
13
+ use Magento \Framework \EntityManager \MetadataPool ;
9
14
10
15
/**
11
16
* CatalogInventory Stock source model
18
23
*/
19
24
class Stock extends AbstractSource
20
25
{
26
+ /**
27
+ * @var MetadataPool
28
+ */
29
+ private $ metadataPool ;
30
+
31
+ /**
32
+ * @param MetadataPool $metadataPool
33
+ */
34
+ public function __construct (
35
+ MetadataPool $ metadataPool
36
+ ) {
37
+ $ this ->metadataPool = $ metadataPool ;
38
+ }
39
+
21
40
/**
22
41
* Retrieve option array
23
42
*
@@ -26,28 +45,57 @@ class Stock extends AbstractSource
26
45
public function getAllOptions ()
27
46
{
28
47
return [
29
- ['value ' => \ Magento \ CatalogInventory \ Model \Stock ::STOCK_IN_STOCK , 'label ' => __ ('In Stock ' )],
30
- ['value ' => \ Magento \ CatalogInventory \ Model \Stock ::STOCK_OUT_OF_STOCK , 'label ' => __ ('Out of Stock ' )]
48
+ ['value ' => StockModel ::STOCK_IN_STOCK , 'label ' => __ ('In Stock ' )],
49
+ ['value ' => StockModel ::STOCK_OUT_OF_STOCK , 'label ' => __ ('Out of Stock ' )]
31
50
];
32
51
}
33
52
34
53
/**
35
54
* Add Value Sort To Collection Select.
36
55
*
37
- * @param \Magento\Eav\Model\Entity\Collection\ AbstractCollection $collection
56
+ * @param AbstractCollection $collection
38
57
* @param string $dir
39
58
*
40
59
* @return $this
41
60
* @since 100.2.4
42
61
*/
43
- public function addValueSortToCollection ($ collection , $ dir = \ Magento \ Framework \ Data \ Collection::SORT_ORDER_DESC )
62
+ public function addValueSortToCollection ($ collection , $ dir = Collection::SORT_ORDER_DESC )
44
63
{
45
- $ collection ->getSelect ()->joinLeft (
46
- ['stock_item_table ' => $ collection ->getTable ('cataloginventory_stock_item ' )],
47
- "e.entity_id=stock_item_table.product_id " ,
48
- []
64
+ $ productLinkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
65
+
66
+ $ collection ->joinField (
67
+ 'child_id ' ,
68
+ $ collection ->getTable ('catalog_product_relation ' ),
69
+ 'child_id ' ,
70
+ 'parent_id= ' . $ productLinkField ,
71
+ null ,
72
+ 'left '
73
+ );
74
+
75
+ $ collection ->joinField (
76
+ 'child_stock ' ,
77
+ $ collection ->getTable ('cataloginventory_stock_item ' ),
78
+ null ,
79
+ 'product_id = entity_id ' ,
80
+ ['stock_id ' => StockModel::DEFAULT_STOCK_ID ],
81
+ 'left '
82
+ );
83
+ $ collection ->joinField (
84
+ 'parent_stock ' ,
85
+ $ collection ->getTable ('cataloginventory_stock_item ' ),
86
+ null ,
87
+ 'product_id = child_id ' ,
88
+ ['stock_id ' => StockModel::DEFAULT_STOCK_ID ],
89
+ 'left '
49
90
);
50
- $ collection ->getSelect ()->order ("stock_item_table.qty $ dir " );
91
+
92
+ $ select = $ collection ->getSelect ();
93
+ $ select ->columns (
94
+ 'IF(SUM(`at_parent_stock`.`qty`), SUM(`at_parent_stock`.`qty`), `at_child_stock`.`qty`) as stock '
95
+ );
96
+ $ select ->group ('e.entity_id ' );
97
+ $ select ->order ("stock $ dir " );
98
+
51
99
return $ this ;
52
100
}
53
101
}
0 commit comments