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

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

4 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

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

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago