Question

I'm trying to have swatch option values selected on the Product View page if only one value exists.

also, I have tried this reference but not working for me https://magento.stackexchange.com/a/176243/68695

like :

enter image description here

Note: when one swatch value available then this value is auto selected and remove validation message

Was it helpful?

Solution

i have solved following way..

open below file if exist otherwise overide

app/design/frontend/Vendor/theme/Magento_Swatches/web/js/swatch-renderer.js

Make changes in the _RenderControls() function as shown below.

after end of this function add like this code

var swatchLength = $('.swatch-option.color').length; // if color attribute 
  if(swatchLength == 1){
     if($('.swatch-attribute').hasClass("color")){
        $('.swatch-option').first().trigger('click');
      }
  }

enter image description here

after run below command

php bin/magento setup:static-content:deploy -f 
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top