77
88namespace Magento \GroupedProduct \Model \Inventory ;
99
10+ use Magento \Catalog \Api \Data \ProductInterface ;
11+ use Magento \Framework \EntityManager \MetadataPool ;
1012use Magento \GroupedProduct \Model \Product \Type \Grouped ;
1113use Magento \Catalog \Api \Data \ProductInterface as Product ;
1214use Magento \CatalogInventory \Api \StockItemCriteriaInterfaceFactory ;
1315use Magento \CatalogInventory \Api \StockItemRepositoryInterface ;
1416use Magento \CatalogInventory \Api \StockConfigurationInterface ;
1517use Magento \CatalogInventory \Observer \ParentItemProcessorInterface ;
1618use Magento \CatalogInventory \Api \Data \StockItemInterface ;
19+ use Magento \GroupedProduct \Model \ResourceModel \Product \Link ;
20+ use Magento \Framework \App \ResourceConnection ;
1721
1822/**
1923 * Process parent stock item for grouped product
@@ -40,22 +44,40 @@ class ParentItemProcessor implements ParentItemProcessorInterface
4044 */
4145 private $ criteriaInterfaceFactory ;
4246
47+ /**
48+ * Product metadata pool
49+ *
50+ * @var MetadataPool
51+ */
52+ private $ metadataPool ;
53+
54+ /**
55+ * @var ResourceConnection
56+ */
57+ private $ resource ;
58+
4359 /**
4460 * @param Grouped $groupedType
4561 * @param StockItemCriteriaInterfaceFactory $criteriaInterfaceFactory
4662 * @param StockItemRepositoryInterface $stockItemRepository
4763 * @param StockConfigurationInterface $stockConfiguration
64+ * @param ResourceConnection $resource
65+ * @param MetadataPool $metadataPool
4866 */
4967 public function __construct (
5068 Grouped $ groupedType ,
5169 StockItemCriteriaInterfaceFactory $ criteriaInterfaceFactory ,
5270 StockItemRepositoryInterface $ stockItemRepository ,
53- StockConfigurationInterface $ stockConfiguration
71+ StockConfigurationInterface $ stockConfiguration ,
72+ ResourceConnection $ resource ,
73+ MetadataPool $ metadataPool
5474 ) {
5575 $ this ->groupedType = $ groupedType ;
5676 $ this ->criteriaInterfaceFactory = $ criteriaInterfaceFactory ;
5777 $ this ->stockConfiguration = $ stockConfiguration ;
5878 $ this ->stockItemRepository = $ stockItemRepository ;
79+ $ this ->resource = $ resource ;
80+ $ this ->metadataPool = $ metadataPool ;
5981 }
6082
6183 /**
@@ -66,7 +88,7 @@ public function __construct(
6688 */
6789 public function process (Product $ product )
6890 {
69- $ parentIds = $ this ->groupedType -> getParentIdsByChild ($ product ->getId ());
91+ $ parentIds = $ this ->getParentEntityIdsByChild ($ product ->getId ());
7092 foreach ($ parentIds as $ productId ) {
7193 $ this ->processStockForParent ((int )$ productId );
7294 }
@@ -122,4 +144,30 @@ private function isNeedToUpdateParent(StockItemInterface $parentStockItem, bool
122144 return $ parentStockItem ->getIsInStock () !== $ childrenIsInStock &&
123145 ($ childrenIsInStock === false || $ parentStockItem ->getStockStatusChangedAuto ());
124146 }
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+ }
125173}
0 commit comments