In magento How to show differerent image while choosing color attribute of a product

In magento How to show differerent image while choosing color attribute of a product

August 22, 2026 · Illustrated author avatarBy Stefan Bogdanescu
In magento How to show differerent image while choosing color attribute of a product

Displaying Different Product Images Based on Color Attribute Selection

In Magento, you can easily show different product images based on the selected color attribute from your product page. This allows customers to visualize how the product looks in various colors before making a purchase. Let’s go through the steps required to achieve this functionality.

Firstly, ensure that your product has a color attribute configured correctly:

  1. Navigate to Catalog > Attributes and click on Add New Attribute.
  2. Enter the attribute name (e.g., “Color”) and select “Dropdown” as the input type for text field.
  3. Click on Manage Label/Options and add your desired colors (e.g., Red, Blue, Green).
  4. For each color option, click on Upload Image to upload a corresponding product image.
  5. Save your changes.

Enabling Color Attribute in Product Configuration

Now let’s set up the color attribute for a specific product:

  1. Go to Catalog > Products, find your product and select it from the list.
  2. In the General Information tab, click on Color under “Attributes” section and choose the desired color option.
  3. Save the changes.

Displaying Color-Selected Image on the Product Page

To display different images based on the selected color attribute:

  1. Open your product view page template (usually located at app/design/frontend/{your_package}/{your_theme}/template/catalog/product/view.phtml).
  2. Locate the following line of code where you’re calling the main product image:
    <img id="image" src="<?php echo $this->helper('catalog/image')->init($product, 'image') ?>" alt="<?php echo $this->getImageLabel() ?>" />
    
  3. Replace it with the following code to get the image corresponding to the selected color attribute:
    <?php if ($this->hasOptions()): ?>
        <div class="color-select-container">
            <?php foreach ($this->getProductOptionByCode('color') as $optionId => $optionValue): ?>
                <label for="color-<?= $optionId ?>">
                    <?= $optionValue['label'] ?>
                </label>
                <input type="radio" id="color-<?= $optionId ?>" name="color" value="<?= $optionId ?>"
                       <?php if ($this->getSelectedColor() == $optionId): ?>checked="checked"<?php endif; ?> />
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
    
    <img id="image" src="<?php echo $this->helper('catalog/image')->init($product, 'color_image_'.$this->getSelectedColor()) ?>" alt="<?php echo $this->getImageLabel() ?>" />
    
  4. In your template file or custom JavaScript, create a function to handle the color selection and update the main image:
    document.querySelectorAll('.color-select-container input[type="radio"]').forEach(radio => {
        radio.addEventListener('change', (event) => {
            const image = document.getElementById('image');
            // Update the image source based on selected color
            image.src = '<?php echo $this->getBaseUrl() ?>'+ '/pub/media/catalog/product/<?= $product->getSku() ?>_color_image_' + event.target.value + '.jpg';
        });
    });
    
  5. Make sure you have the image files named correctly (e.g., sku_color_image_1.jpg, sku_color_image_2.jpg) in your media directory.

Conclusion

By following these steps, you can now display different product images based on the selected color attribute directly from the dropdown menu on your Magento product page. This enhances user experience and provides a better visual representation of your products’ available options.

For more advanced customizations and best practices in Magento development, feel free to explore our other blog posts and resources at Laravel Company.

Catalog Themes Configuration Customers

Our network