Hello Magento Friends,
Today I am going to throw light on How to Get Bundle Product Option Image in Magento 2.
Magento 2 supports various types of products one of them is bundled products. Bundled products are a group of products that customers can choose their own products to make a bundle. Learn – How to Create Bundled Product Programmatically in Magento 2
Bundle products have an image gallery on the product page. You can add product options images to the main image gallery. When the user selects a particular product option, it should update the selected option as the main image from the gallery.
To fulfill the above requirements, follow the below steps.
Steps to Get Bundle Product Option Image in Magento 2:
Step 1: Move to the below path
app\code\Vendor\Extension\Block\Product\Type\Bundleoption.php
Add the code as mentioned below
<?php namespace Vendor\Extension\Block\Product\Type; Class Bundleoption extends \Magento\Bundle\Block\Catalog\Product\View\Type\Bundle { protected $helperbundleimage; public function __construct( \Magento\Catalog\Block\Product\Context $context, \Magento\Bundle\Model\Product\PriceFactory $productPrice, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\Stdlib\ArrayUtils $arrayUtils, \Magento\Catalog\Helper\Product $catalogProduct, \Magento\Framework\Locale\FormatInterface $localeFormat, array $data = [] ) { $this->helperbundleimage = $context->getImageHelper(); parent::__construct( $context, $arrayUtils, $catalogProduct, $productPrice, $jsonEncoder, $localeFormat, $data ); } }
Step 2: Now Create a Simple function with 2 parameters, or else you can get the product object from where you can get it.
public function getBundleOptionImage($_product, $type) { return $this->helperbundleimage ->init($_product,$type, ['height' => 140,'width'=> 140]) ->getUrl(); }
Conclusion:
Hence, this way you can Get Bundle Product Option Image in Magento 2. If you face any difficulty, let me know in the comment box. Share the article with your friends. I will be back with another Magento solution, till then stay updated with us.
Happy Coding!