Magento 2.3 How to Auto Select Swatch Option Values if Only One Exists in Product View
-
04-03-2021 - |
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 :
Note: when one swatch value available then this value is auto selected and remove validation message
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');
}
}
after run below command
php bin/magento setup:static-content:deploy -f
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange