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

2 Answers 2


i have solved following way..

open below file if exist otherwise overide


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){

enter image description here

after run below command

php bin/magento setup:static-content:deploy -f 
  • I am using size attribute as dropdown and this code is not working help
    – NsVentures
    Jul 4, 2020 at 23:46

add this code anywhere on product page

 require(['jquery', 'jquery/ui'], function($){ 
     $(document).on('swatch.initialized', function() {
        var swatchLength = $('.swatch-attribute').length;
        if(swatchLength >= 1){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.