Magento 2 Get Parent Product id from child id.

Magento 2 Get Configurable product id from Child id of configurable. When you have Child id of the P product is available you can simply get the parent config product id.

Create simple block to get parent product id,

<?php
namespace Rbj\Product\Block;

class Product extends \Magento\Framework\View\Element\Template
{
	public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
        array $data = []
    ) {
        $this->configurable = $configurable;
        parent::__construct($context, $data);
    }

    /**
     * @param int $childId
     * @return int
     */
    public function getParentProductId($childProductId)
    {
            $parentConfigObject = $this->configurable->getParentIdsByChild($childProductId);
	    if($parentConfigObject) {
		return $parentConfigObject[0];
	    }
	    return false;
    }
}

now call the function in template file to get Parent Id,

$childId = 10;
echo $block->getParentProductId($childId);

 

 

Create a Custom module using Graphql in Magento 2.

GraphQL is a new concept from Magento 2.3 and Many Magento Enthusiastic have eagerly waiting for the simple module using GraphQL and how GraphQL interacts with Magento 2 and why GraphQL is used for Magento 2 and so many other stuff around GraphQl.

I want to give a demo of a simple module of GraphQL in Magento 2. How GraphQl is actually used in Magento 2 using programmatic way and so on. Continue reading “Create a Custom module using Graphql in Magento 2.”

GraphQL in Magento 2.

An Articles for the GraphQL in Magento 2.

From the Magento 2.3 and Higher Version, Out of the box, GraphQL Feature will be available in Native installation. GraphQL is the Type of API for the front end UI with a Headless Approach. Continue reading “GraphQL in Magento 2.”