How To

How to Get Bundle Product Option Image in Magento 2

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!

Click to rate this post!
[Total: 8 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • Hi Dhiren

    Let’s say I wanted to display my product option just like the product list view. with all the information and a checkbox to add it to the bundle - is this something you could help me with?

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

13 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago