I want to get parent product data for configurable products.
I have parent id
How do I get it?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI want to get parent product data for configurable products.
I have parent id
How do I get it?
I Got the solution
use \Magento\Catalog\Model\ProductRepository;
use \Magento\ConfigurableProduct\Model\Product\Type\Configurable;
Then in construct function use
public function __construct(ProductRepository $_productloader,Configurable $configurable) {
$this->configurable=$configurable;
$this->_productloader=$_productloader;
}
get parent product id by product id
$productId='get your product id'
$parentId=$this->configurable->getParentIdsByChild($productId);
$parentId gives an array
then get parent product data by
$parentProductData=$this->_productloader->getById($parentId[0]);
This worked for me.