In Magento, there are three product type: bundle, configurable and grouped. They have some children. Today, we will try to get children from product id and try to get parent Ids from product id.
1. Bundle product:
You can take a look at the class Magento\Bundle\Model\Product\Type, it has two functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/** * Retrieve Required children ids * Return grouped array, ex array( * group => array(ids) * ) * * @param int $parentId * @param bool $required * @return array */ public function getChildrenIds($parentId, $required = true) { return $this->_bundleSelection->getChildrenIds($parentId, $required); } /** * Retrieve parent ids array by required child * * @param int|array $childId * @return array */ public function getParentIdsByChild($childId) { return $this->_bundleSelection->getParentIdsByChild($childId); } Configurable product: You can see the class Magento\ConfigurableProduct\Model\Product\Type\Configurable, it also has two functions: /** * Retrieve Required children ids * Return grouped array, ex array( * group => array(ids) * ) * * @param array|int $parentId * @param bool $required * @return array */ public function getChildrenIds($parentId, $required = true) { return $this->_catalogProductTypeConfigurable->getChildrenIds($parentId, $required); } /** * Retrieve parent ids array by required child * * @param int|array $childId * @return array */ public function getParentIdsByChild($childId) { return $this->_catalogProductTypeConfigurable->getParentIdsByChild($childId); } |
2. Grouped product:
- You can see the class Magento\GroupedProduct\Model\Product\Type\Grouped:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/** * Retrieve Required children ids * Return grouped array, ex array( * group => array(ids) * ) * * @param int $parentId * @param bool $required * @return array * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function getChildrenIds($parentId, $required = true) { return $this->productLinks->getChildrenIds( $parentId, \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED ); } /** * Retrieve parent ids array by requested child * * @param int|array $childId * @return array */ public function getParentIdsByChild($childId) { return $this->productLinks->getParentIdsByChild( $childId, \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED ); } |
- So you can use those class and call the function getChildrenIds or getParentIdsByChild. However, with this solution, you need to know product type. If you don’t know the product type, you can load product and call the function getTypeInstance like this:
1 |
$product->getTypeInstance()->getParentIdsByChild($child->getId()); |
The steps I mention above is the shortest process for you to Get Parent Product Ids in Magento 2. With this guide, you can manage the Product Ids in Magento 2 easily. Every store has a Product Ids in Magento 2 with many attributes.
Thank you for reading this post.