Opencart 4 · 7 min read · Updated

Color and Image Swatches for OpenCart 4: Color Dots, Pattern Tiles and Size Buttons Instead of Dropdowns (Free Extension)

A free OpenCart 4 extension that turns option dropdowns into color dots, image tiles and size buttons, and switches the main photo when a color is picked. Tested on 4.1.0.4.

Opencart 4 file manager search

A shopper opens a T-shirt in your OpenCart 4 store, sees a “Color” dropdown that says “— Please Select —“, and has to open it to find out which colors exist. On a phone that is two taps before they see anything. Clothing, shoes, furniture and phone cases all sell on color, and a dropdown hides it. OpenCart 4 color swatches put every choice on the page.

The free Color & Image Swatches extension turns chosen select and radio options into color dots, image tiles or size buttons. It shows the picked value next to the option name, and it changes the main product photo when a color is picked. It is built on an OpenCart event, so no core file is edited, and the original dropdowns stay in the form, so add to cart, required checks and option prices keep working exactly as before. These OpenCart 4 color swatches were tested on OpenCart 4.1.0.4 with the Webocreation Theme and the Image Zoom extension.

Download: swatches.ocmod.zip (tested on OpenCart 4.1.0.4).

OpenCart 4 product page with color dots for Black, White, Navy and Red, pattern tiles for Print and Small, Medium, Large buttons for Size, with the navy T-shirt shown as the main image
Navy is picked, so the main image shows the navy shirt and the label reads “Color: Navy”.

What the OpenCart 4 color swatches extension does

  • Color dots for values whose name is in your color list, such as Red, Navy or Black. You keep the list in the settings as one “Name = #hex” line per color.
  • Image tiles for values that have an image in Catalog > Options, such as fabric prints, wood finishes or patterns.
  • Text buttons for everything else, which works well for sizes such as Small, Medium and Large.
  • The picked value next to the option name, for example “Color: Navy”, so shoppers see their choice without reading the swatches.
  • Main image swap. Picking Navy shows the first product image whose file name contains “navy”, such as tee-navy.png.
  • Prices stay visible. A value with a price shows it in the tooltip and to screen readers, for example “Large (+$4.40)”.
  • Keyboard support. Each option is a radio group. Tab moves into it and the arrow keys pick the next or previous value.

Here is the same product without the extension: three plain controls, and nothing tells the shopper what colors exist until they open the dropdown.

The same OpenCart 4 product options before the extension: a Color dropdown, Print radio buttons with small images and a Size dropdown
Before: core OpenCart 4.1 option controls.

Install the extension

  1. Download swatches.ocmod.zip. Don’t unzip it.
  2. In the admin, go to Extensions > Installer, click Upload and choose the zip.
  3. Click the green Install button next to “Color & Image Swatches” in the installed list.
  4. Go to Extensions > Extensions, choose Modules from the dropdown, find Color & Image Swatches and click the green Install (plus) button.
  5. Click the blue Edit (pencil) button, tick the options that should show swatches, switch Status on and click Save.

You don’t need to add it to a layout. It runs on every product page that uses one of the ticked options.

Settings

Color and Image Swatches settings page in OpenCart 4 admin with status, a checklist of select and radio options, the color names textarea, shape, size, show the picked value and change the main image
The settings page lists every select and radio option with the number of products that use it.
SettingWhat it does
Show as swatchesThe options that get swatches. Only select and radio options are listed, because a swatch picks exactly one value. Options you don’t tick stay as they are.
Color namesOne color per line as Name = #hex, for example Navy = #1b2a4a. Matching ignores letter case. The extension ships with 15 common colors, and a line in the wrong format is refused when you save, with its line number.
ShapeCircle or rounded square.
Size (px)From 20 to 80. The default of 36 px is easy to tap on a phone.
Show the picked valuePrints the picked value after the option name, as in “Size: Medium”.
Change the main imageTurns the image swap on or off.

Set up colors, images and photos

Color dots

Name your option values after colors, such as Black, White, Navy and Red, and make sure each name has a line in the color list. If you sell “Forest Green” or “Heather Grey”, add those names with the hex code you want. A value with no matching line shows as a text button, which is still better than a dropdown.

Image tiles

Open Catalog > Options, edit the option and give each value an image. OpenCart already has an image field per option value. The extension uses the original file and resizes it to twice the swatch size, so tiles stay sharp on high-density screens. An option value image wins over a color name, so a “Red” value with a fabric photo shows the photo.

OpenCart 4 Catalog Options form for the Print option with images for the Plain, Stripes and Dots values
Option value images in Catalog > Options become image tiles.

Main image swap

Upload one photo per color as an additional product image and put the color name in the file name: webo-tee-navy.png, webo-tee-red.png and so on. The match uses whole words, so “Navy Blue” matches tee-navy-blue.jpg and Tee_Navy_Blue_2.png, while “Red” doesn’t match shredder.jpg. The main image is checked too, so the default photo can belong to a color as well.

How it works

On install, the extension registers one event on the product page view. Nothing else is hooked.

public function install(): void {
    $this->load->model('setting/event');

    $this->model_setting_event->addEvent([
        'code'        => 'swatches',
        'description' => 'Color & Image Swatches: swatches on product pages',
        'trigger'     => 'catalog/view/product/product/after',
        'action'      => 'extension/swatches/module/swatches.product',
        'status'      => 1,
        'sort_order'  => 0
    ]);
}

The event handler reads the options OpenCart already prepared for the page. Core leaves out values that are sold out when “Subtract Stock” is on, so sold-out colors never get a swatch. In our test the Green value had 0 in stock and didn’t appear. For each value the handler decides between image, color and text, finds the matching product photo, and appends a small script with that data to the page.

This is the file name match:

private function matchImage(string $name, array $images): array {
    $slug = trim(preg_replace('/[^a-z0-9]+/', '-', mb_strtolower($name)), '-');

    if ($slug === '') {
        return [];
    }

    foreach ($images as $image) {
        $base = mb_strtolower(pathinfo($image['file'], PATHINFO_FILENAME));
        $words = '-' . trim(preg_replace('/[^a-z0-9]+/', '-', $base), '-') . '-';

        if (str_contains($words, '-' . $slug . '-')) {
            return $image;
        }
    }

    return [];
}

In the browser, the script builds a row of buttons for each option and hides the original select or radio inputs without removing them. Picking a swatch sets the hidden input and fires its change event, so anything else listening to the option, such as a price updater, still hears it:

function pick(value) {
  if (group.type === 'select') {
    holder.value = value;
    holder.dispatchEvent(new Event('change', { bubbles: true }));
  } else {
    var input = holder.querySelector('input[value="' + value + '"]');
    if (input) { input.checked = true; input.dispatchEvent(new Event('change', { bubbles: true })); }
  }
  // Core only clears "required" errors on the next add to cart; clear this one now.
  var error = document.getElementById('error-option-' + group.id);
  if (error) { error.classList.remove('d-block'); }
  holder.classList.remove('is-invalid');
  sync();
}

Because the inputs are still in the form, core does the rest. When we added the shirt to the cart, the cart line read “Color: Red – Print: Stripes – Size: Medium” with the option prices included.

What we found when testing

  • Required options. Clicking Add to Cart with nothing picked shows core’s “Color required!” and “Size required!” messages under the swatches. Core only clears those messages on the next add to cart, so the extension clears the one for an option as soon as a value is picked.
  • Image Zoom. The Product Image Zoom extension keeps its own record of the current image for the zoom lens and the popup. If the swatch changed the image behind its back, the popup would still open the old photo. So when Image Zoom is active, the swatch clicks the matching Image Zoom thumbnail instead. The thumbnail outline, lens and popup all moved to the navy shirt.
  • Option prices include tax when the store shows prices with tax, so “Stripes” showed “+$5.60” for a $3.00 option at 20% VAT and an eco tax. That is core’s price, not the extension’s.
  • Phones. The swatches wrap onto as many lines as they need, and the 36 px default is a comfortable tap target.
Color and size swatches on a phone-sized OpenCart 4 product page with Red and Large picked
On a 390 px wide phone screen.

Limitations

  • Checkbox options are left alone, because a shopper can tick several values and swatches pick one.
  • Color names are matched against the option value name in the shopper’s language. On a multilingual store, add the translated names to the color list too, for example Rouge = #d32f2f.
  • Option value images are shared by every product that uses the option, because that is how OpenCart stores them. For a different photo per product, use the main image swap.
  • Without JavaScript the shopper sees the normal core dropdowns and radio buttons, which still work.
  • A theme that renames core’s input-option- IDs or the .magnific-popup image link needs a small change in the script.
Rupak Nepali
Written byRupak Nepali

Rupak Nepali is a web developer, manage Webocreation website and the author of six books:
Getting Started with OpenCart Module Development (Packt, 2013),
OpenCart Theme and Module Development (Packt, 2015),
OpenCart 4: Dev Guide for Themes & Extensions (2024),
OpenCart 4: User Manual (2025),
Cloudflare for the Enterprise Website (2026) and
The Technical Web Guide for Marketers (2026).
Rupak has built OpenCart stores and extensions since 2010, has published more than 600 tutorials on Webocreation, and shares free OpenCart extensions on GitHub.
Find Rupak on
GitHub | Amazon | LinkedIn | X | YouTube.

Leave a Reply

Your email address will not be published. Required fields are marked *