Magento – How to Auto Select The Color as Swatch Option for Products with Only One Color (on Magento 2.1 Product View)

color-swatchesmagento-2.1swatches

I'm trying to have swatch option value of color auto-selected for Products that only have one color.

If a user selects some product from the category page, the product page is displayed with attributes like Color, Shape, and Size. Some of the products only have one color though, but still require the user to click-select it. I want the color to be automatically selected for all single-colored products when the pages load.

Best Answer

This is an old question, but I ran into the same issue today. I solved it using jquery. I added this to the view.phtml template and it looks to work for me. Just posting it here in case someone else has a similar problem.

$j('.configurable-swatch-list').each(function() {
    if ($j(this).find('li').length == 1) {
        $j(this).find('li:first-child a span').trigger("click");
    }
});

This will target all configurable swatch lists, you can change the code to just look for the particular swatch list you care about.

Related Topic