55 */
66namespace Magento \CatalogInventory \Model \Source ;
77
8+ use Magento \Catalog \Api \Data \ProductInterface ;
9+ use Magento \CatalogInventory \Model \Stock as StockModel ;
810use 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 ;
914
1015/**
1116 * CatalogInventory Stock source model
1823 */
1924class Stock extends AbstractSource
2025{
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+
2140 /**
2241 * Retrieve option array
2342 *
@@ -26,28 +45,57 @@ class Stock extends AbstractSource
2645 public function getAllOptions ()
2746 {
2847 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 ' )]
3150 ];
3251 }
3352
3453 /**
3554 * Add Value Sort To Collection Select.
3655 *
37- * @param \Magento\Eav\Model\Entity\Collection\ AbstractCollection $collection
56+ * @param AbstractCollection $collection
3857 * @param string $dir
3958 *
4059 * @return $this
4160 * @since 100.2.4
4261 */
43- public function addValueSortToCollection ($ collection , $ dir = \ Magento \ Framework \ Data \ Collection::SORT_ORDER_DESC )
62+ public function addValueSortToCollection ($ collection , $ dir = Collection::SORT_ORDER_DESC )
4463 {
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 '
4990 );
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+
5199 return $ this ;
52100 }
53101}
0 commit comments