Change the images shown when selecting different configurable products (ex. different color)

Change the images shown when selecting different configurable products (ex. different color)

October 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Change the images shown when selecting different configurable products (ex. different color)

Solving Image Issues for Magento Configurable Products

As you’re working on your first Magento build, you’ve already made great progress by figuring out how to display option images for bundled products. Now, let’s tackle the challenge of showing images for configurable products as well.

Understanding Configurable Products

Configurable products are built from simple products that represent each iteration of available options. For example, if you have a chair with 3 upholstery and 2 armrest choices (6 simple products), each simple product will correspond to one specific combination of these options.

Your current approach—uploading images for each simple product and associating them by label—is a good start. However, the configurable product’s option selects are injected onto the page via JavaScript in the js/varien/configurable.js file.

Retrieving Image URLs from Simple Products

To associate these image URLs with the option selects, we’ll need to work with the JSON configuration data generated by the getJsonConfig() method. This data includes information about the simple products associated with each configurable product’s options.

Here’s a simplified example of how you might structure this JSON:

{
  "attributes": {
    "123": {
      "id": 123,
      "code": "upholstery",
      "position": 0,
      "label": "Upholstery Color",
      "values": [
        {"value_id": 456, "label": "Black", "image_url": "/path/to/black.png"},
        // More options...
      ]
    },
    // More attributes...
  }
}

In this JSON, each value object represents a simple product’s option and includes an image URL.

Integrating Product Images with Option Selects

To display these images next to the option selects, you’ll need to modify the catalog/product/view/type/option/configurable.phtml file. This is where the option select elements are rendered for each configurable product’s attribute.

First, make sure you have loaded the JSON configuration data in your JavaScript:

<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>

Next, modify the foreach loop that generates the option select elements to include image URLs based on the selected value_id:

<?php foreach ($_attributes as $_attribute): ?>
    <!-- Other existing code -->
    <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
        <?php foreach ($_attribute->getValues() as $_value): ?>
            <option value="<?php echo $_value->getValueId() ?>" <?php if($_value->getIsDefault()): ?>selected="selected"<?php endif; ?>>
                <?php echo $this->escapeHtml($_value->getLabel()) ?>
            </option>
        <?php endforeach; ?>
    </select>

    <!-- Add this line to display the image url -->
    <img src="<?php echo $this->helper('catalog/image')->init($_attribute, 'image', $_value->getValueId())->resize(100); ?>" alt="<?php echo $_value->getLabel() ?>">

<?php endforeach; ?>

In this modified code, we’re using Magento’s catalog/image helper to generate the image URL based on the simple product’s value_id. The init() method initializes an image object with the current attribute and the specified image type (‘image’). The resize() method then resizes the image to a width of 100 pixels.

Conclusion

Displaying images for configurable products requires working with the JSON configuration data generated by Magento’s getJsonConfig() method. By modifying the catalog/product/view/type/option/configurable.phtml file, you can associate these image URLs with the option select elements using the catalog/image helper.

Remember that the actual implementation may require additional logic to handle cases like multiple images per simple product or dynamic image resizing based on your theme’s design. Be sure to thoroughly test your solution across various browsers and devices.

For more information on customizing Magento themes, I recommend checking out our comprehensive guide at Laravel Company. It covers best practices for modifying existing templates like the configurable.phtml file you’ve been working with.

Catalog API Configuration Errors

Our network