Colour Filter Swatch

Create a colour swatch list which appears in the sidebar. This article shows you how to replace text based specifics with colour images.

Wireframe

Review Stars


Preparation Checklist

Before you start this tweak, it's a good idea to run through our preparation checklist below:

  • Read through the Getting Started to get a better sense of how the control panel, Database and Front End store interact.
  • Learn our recommended Simple Workflow. This makes the implementation process as easy as possible.
  • Create a new Staging Theme for this tweak. This allows you to preview any changes before they are visible to live customers.

Coding Instructions

Step 1: Open the sidebar template

The sidebar template can be found here /httpdocs/assets/themes/[THEME-NAME]/templates/cms/includes/sidebar.template.html.

Step 2: Add the code into the template

Replace the code within the [%param *body%] for [%PRODUCT_FILTER%] with the below:

[%if [@filter_name@] eq 'Colour' %]
    [%data id:'selected' if:'==' value:'1'%]
    [%param *if_true%]
        <li class="filter-remove filter filter-colour">
            <a href="[@remove_single_url@]" rel="nofollow" data-toggle="tooltip" title="[@name@]"
                style="
                [%if [@variation_swatch@] ne ''%]
                    background-color:[@variation_swatch@]
                [%else%]
                    background-image:url('[@config:homeurl@]/assets/itmspecific/[@id@].png')
                [%/if%]">
            </a>
        </li>
    [%/param%]
    [%param *if_false%]
        <li class="filter filter-colour">
            <a href="[@url@]" rel="nofollow" data-toggle="tooltip" title="[@name@]"
                style="
                [%if [@variation_swatch@] ne ''%]
                    background-color:[@variation_swatch@]
                [%else%]
                    background-image:url('[@config:homeurl@]/assets/itmspecific/[@id@].png')
                [%/if%]">
            </a>
        </li>
    [%/param%]
[%/data%]
[%else%]
    [%data id:'selected' if:'==' value:'1'%]
        [%param *if_true%]
            <li class="filter-remove filter"><a href="[@remove_single_url@]" class="list-group-item" rel="nofollow">
                [@name@] <span class="text-muted"></span></a>
            </li>
        [%/param%]
        [%param *if_false%]
            <li class="filter">
                <a class="list-group-item" href="[@url@]"> [@name@] <span class="text-muted"></span></a>
            </li>
        [%/param%]
    [%/data%]
[%/if%]

Notes On The Above Code

This code updates the filter for the "Colour" specific. In order for the above to work, the specific name itself must be set to "Colour".

Firstly, the code tests to see whether an image exists for that particular specific value, and displays it if so. In the absence of an image, it displays the "swatch" value for the specific instead.

An important point is that the images that are displayed for each specific value must be PNG files, otherwise they won't show.

Step 3: Add styling

Add the following CSS to the appropriate stylesheet:

.filter-colour {
    display: inline-block;
    padding: 10px 1px 0 8px;
}
.list-group > li + .filter.filter-colour > a:before {
    content: '';
}
.list-group > li + .filter.filter-colour > a {
    display: block;
    width: 30px;
    height: 30px;
    border-radius: 15px;
    box-shadow: 1px 1px 2px #D6D6D6;
}
.filter-remove > a {
    border: 2px solid #AFAFAF;
}

Step 4: Shuffle elements or update styles to suit

Update the CSS to suit, if necessary - for example, the padding between each swatch may need to be adjusted.


Final Result

Was this article useful?

Be notified when this page is updated. Optional.