how to create the color swatches in magento and how to show in product detail page?

how to create the color swatches in magento and how to show in product detail page?

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to create the color swatches in magento and how to show in product detail page?

How to Implement Color Swatches in Magento and Display Them on the Product Detail Page

Are you looking to enhance your product detail page with interactive color swatches? In this guide, we’ll walk through the process of creating color swatches in Magento (version 1.7.0.2) and displaying them beautifully.

Step 1: Prepare Your Color Attributes

First, ensure that you have created the necessary attributes for the colors you want to display. Go to Catalog > Attributes > Manage Attributes. Click on “Add New Attribute” and fill in the appropriate details like attribute code, label, etc. For our example, let’s create an attribute called ‘Color’.

Step 2: Create Swatches

Next, we need to create swatch images for each color option.

  1. Go to Catalog > Attributes > Manage Options.
  2. Find your Color attribute and click on “Edit.”
  3. In the “Manage Options” section, click on “Add Option.”
  4. Enter a value (e.g., ‘Red’), then browse and upload an image file representing that color.
  5. Click “Save Attribute.”

Step 3: Display Swatches on Product Page

Now let’s modify your product detail page template to display these swatches.

  1. Navigate to your theme directory under app/design/frontend/{package}/{theme}.
  2. Open the file template/catalog/product/view.phtml.
  3. Search for the line containing <div class="product-categories"> and add the following code just above it:
<?php if ($this->getProduct()->isConfigurable()): ?>
    <div class="color-swatches">
        <?php $swatches = $this->helper('catalog/product')->getColors($this->getProduct()); ?>
        <?php foreach ($swatches as $code => $data): ?>
            <a href="#" title="<?php echo $this->htmlEscape($data['label']) ?>"
                style="background-color: <?php echo $data['value']; ?>">
                <!-- Swatch Image -->
                <img src="<?php echo $data['image_url']; ?>" />
            </a>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

This code retrieves the color swatches for the current product and displays them using their respective colors.

Step 4: CSS Styling

Finally, style your new color swatch area in your theme’s main CSS file (usually skin/frontend/{package}/{theme}/css/styles.css).

.color-swatches a {
    display: inline-block;
    margin-right: 5px;
}

.color-swatches img {
    width: 30px;
    height: 30px;
}

Conclusion

With these steps, you should now have fully functional color swatches that are displayed on your product detail page. This enhancement not only improves the visual appeal of your site but also provides a better user experience.

Remember to always backup your files before making modifications, and test thoroughly on a staging environment first.

If you’re looking for advanced customization or need help with other Magento features, feel free to explore our expert services at LaravelCompany.com. Our team specializes in Magento development and can provide tailored solutions for your e-commerce needs.

Magento 1 Catalog Themes Configuration

Our network