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 […]