7
7
8
8
namespace Magento \GroupedProduct \Model \Inventory ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
11
+ use Magento \Framework \EntityManager \MetadataPool ;
10
12
use Magento \GroupedProduct \Model \Product \Type \Grouped ;
11
13
use Magento \Catalog \Api \Data \ProductInterface as Product ;
12
14
use Magento \CatalogInventory \Api \StockItemCriteriaInterfaceFactory ;
13
15
use Magento \CatalogInventory \Api \StockItemRepositoryInterface ;
14
16
use Magento \CatalogInventory \Api \StockConfigurationInterface ;
15
17
use Magento \CatalogInventory \Observer \ParentItemProcessorInterface ;
16
18
use Magento \CatalogInventory \Api \Data \StockItemInterface ;
19
+ use Magento \GroupedProduct \Model \ResourceModel \Product \Link ;
20
+ use Magento \Framework \App \ResourceConnection ;
17
21
18
22
/**
19
23
* Process parent stock item for grouped product
@@ -40,22 +44,40 @@ class ParentItemProcessor implements ParentItemProcessorInterface
40
44
*/
41
45
private $ criteriaInterfaceFactory ;
42
46
47
+ /**
48
+ * Product metadata pool
49
+ *
50
+ * @var MetadataPool
51
+ */
52
+ private $ metadataPool ;
53
+
54
+ /**
55
+ * @var ResourceConnection
56
+ */
57
+ private $ resource ;
58
+
43
59
/**
44
60
* @param Grouped $groupedType
45
61
* @param StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory
46
62
* @param StockItemRepositoryInterface $stockItemRepository
47
63
* @param StockConfigurationInterface $stockConfiguration
64
+ * @param ResourceConnection $resource
65
+ * @param MetadataPool $metadataPool
48
66
*/
49
67
public function __construct (
50
68
Grouped $ groupedType ,
51
69
StockItemCriteriaInterfaceFactory $ criteriaInterfaceFactory ,
52
70
StockItemRepositoryInterface $ stockItemRepository ,
53
- StockConfigurationInterface $ stockConfiguration
71
+ StockConfigurationInterface $ stockConfiguration ,
72
+ ResourceConnection $ resource ,
73
+ MetadataPool $ metadataPool
54
74
) {
55
75
$ this ->groupedType = $ groupedType ;
56
76
$ this ->criteriaInterfaceFactory = $ criteriaInterfaceFactory ;
57
77
$ this ->stockConfiguration = $ stockConfiguration ;
58
78
$ this ->stockItemRepository = $ stockItemRepository ;
79
+ $ this ->resource = $ resource ;
80
+ $ this ->metadataPool = $ metadataPool ;
59
81
}
60
82
61
83
/**
@@ -66,7 +88,7 @@ public function __construct(
66
88
*/
67
89
public function process (Product $ product )
68
90
{
69
- $ parentIds = $ this ->groupedType -> getParentIdsByChild ($ product ->getId ());
91
+ $ parentIds = $ this ->getParentEntityIdsByChild ($ product ->getId ());
70
92
foreach ($ parentIds as $ productId ) {
71
93
$ this ->processStockForParent ((int )$ productId );
72
94
}
@@ -122,4 +144,30 @@ private function isNeedToUpdateParent(StockItemInterface $parentStockItem, bool
122
144
return $ parentStockItem ->getIsInStock () !== $ childrenIsInStock &&
123
145
($ childrenIsInStock === false || $ parentStockItem ->getStockStatusChangedAuto ());
124
146
}
147
+
148
+ /**
149
+ * Retrieve parent ids array by child id
150
+ *
151
+ * @param int $childId
152
+ * @return string[]
153
+ */
154
+ private function getParentEntityIdsByChild ($ childId )
155
+ {
156
+ $ select = $ this ->resource ->getConnection ()
157
+ ->select ()
158
+ ->from (['l ' => $ this ->resource ->getTableName ('catalog_product_link ' )], [])
159
+ ->join (
160
+ ['e ' => $ this ->resource ->getTableName ('catalog_product_entity ' )],
161
+ 'e. ' .
162
+ $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField () . ' = l.product_id ' ,
163
+ ['e.entity_id ' ]
164
+ )
165
+ ->where ('l.linked_product_id = ? ' , $ childId )
166
+ ->where (
167
+ 'link_type_id = ? ' ,
168
+ Link::LINK_TYPE_GROUPED
169
+ );
170
+
171
+ return $ this ->resource ->getConnection ()->fetchCol ($ select );
172
+ }
125
173
}
0 commit comments