Question

I want to get Product id in this file:-

vendor\magento\module-catalog\view\frontend\templates\product\image_with_borders.phtml

Magento version 2.2.3

Was it helpful?

Solution

Ok, I did it myself:-

Go to file \vendor\magento\module-catalog\Helper\Image.php and add function:-

public function getProductId()
    {
        return $this->_product->getId();


    }

Add variable to $data:-

vendor\magento\module-catalog\Block\Product\ImageBuilder.php

'id' => $helper->getProductId(),

Add your code in phtml file:-

echo $block->getId();

I will be overriding my files now in my custom module

<preference for="Magento\Catalog\Helper\Image" type="Vendor\Module\Helper\Image" />

Code of my custom file -

    use Magento\Framework\App\Helper\AbstractHelper;

/**
 * Catalog image helper
 *
 * @api
 * @SuppressWarnings(PHPMD.TooManyFields)
 * @since 100.0.2
 */
class Image extends \Magento\Catalog\Helper\Image
{    
    public function getProductId()
    {
        return $this->_product->getId();

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top