- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2018
02:23 AM
12-28-2018
02:23 AM
How to check product has parent id using product object ($product = $item->getProduct();)
Please provide suggestions
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2018
03:33 AM
12-28-2018
03:33 AM
Re: How to check product has parent id using product object ($product = $item->getProduct();)
$product->getTypeInstance()->getParentIdsByChild($child->getId());
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2018
08:57 PM
12-28-2018
08:57 PM
Re: How to check product has parent id using product object ($product = $item->getProduct();)
Hello @anand_v
Try this
$productId = 10; //this is child product id
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($productId); if (isset($product[0])) { //this is parent product id.. echo $product[0]; }
Note: $objectManager is used here for demonstration purpose only.
If you find my answer useful, Please click Kudos & Accept as Solution.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2018
10:17 AM
12-29-2018
10:17 AM
Re: How to check product has parent id using product object ($product = $item->getProduct();)
Hi,
protected $_catalogProductTypeConfigurable; public function __construct( \Magento\Catalog\Block\Product\Context $context, //for getting parent id of simple \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable, array $data = [] ) { //for getting parent id of simple $this->_catalogProductTypeConfigurable = $catalogProductTypeConfigurable; parent::__construct($context, $data); } public function getParentProductData($id){ $parentByChild = $this->_catalogProductTypeConfigurable->getParentIdsByChild($id); if(isset($parentByChild[0])){ //set id as parent product id... $id = $parentByChild[0]; } return $id; }
Prema M