1

I have injected.

Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable

In my constructor as $configProductModel

$this->configProductModel->getParentsIdsByChild($product->getId());
// outputs [];

I know this product to be a simple Product and I'm trying to load the configurable Product so I can get the Parent SKU

1 Answer 1

1

To get configure product id from its the simple product use below code.

namespace Vendor\Frontendsample\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $configure;
    protected $resultPageFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configure
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configure
    ){
        $this->configure = $configure;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $productId = 12;//your simple product id of configure product
        $parentIds = $this->configure->getParentIdsByChild($productId);
        $parentId = array_shift($parentIds);
        print_r($parentId);
    }
}

If you getting nothing from above code, then you should check child(simple) product id of parent(configure) product id, may be its wrong or may be something wrong with your configure product in admin side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.