How to resize product image in magento 2?

To resize product images in Magento 2, You need to refer to the code snippet for resizing product images at any place in the file.

Magento Class file Magento\Catalog\Helper\Image is used to resize the product image.

Call Init() function and pass parameters,

The first parameter is the $product Object,
The second parameter is the product id, You can be called any product id like thumbnail, or small_image (Check the list of image id under the theme etc/view.xml file)
The third parameter as array is optional,

For Custom Image resizing refer to the blog, Resize the custom image in Magento 2

Continue reading “How to resize product image in magento 2?”

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.”