How to resize custom image in magento 2?

We can resize the custom image in Magento 2 with a simple coding snippet.

Many times we need to resize images in the Magento project for a custom module requirement or category images.

For Image resizing, we need to create the Block file under the module and called the function under the template file. Continue reading “How to resize custom image in magento 2?”

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);