Magento – Magento2 how to remove “choose an option” in configurable products drop down
configurable-productfrontendmagento2option
How to remove "choose an option" in configurable products drop down and in the drop-down menu have the first attribute value be the top choice please if anyone idea reply me
Here You Can Make Attribute Selected...If DorpDown Have Only One Attribute....
Bellow of This code
var spConfig = new Product.Config(<?php echo $_jsonConfig ?>);
Put this code....here..
//we create new function
spConfig.setInitialState = function (dropdown_id)
{
//select dropdown
var dropdown = $(dropdown_id);
//remove empty option from dropdown so it is not selectable after initial selection
// dropdown[0].remove();
//alert(dropdown.length);
if (dropdown.length == 2) {
dropdown[0].remove();// for the removing choose an option
}
//change selections in dropdowns
for (index = 0; index < dropdown.length; index++)
{
if (dropdown[index].value != "")
{
dropdown.selectedIndex = index;
var element = dropdown;
var event = 'change';
//fire events
if (document.createEventObject)
{
var evt = document.createEventObject();
return element.fireEvent('on' + event, evt)
} else
{
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true);
return !element.dispatchEvent(evt);
}
}
}
};
<?php foreach ($_attributes as $_attribute): ?>
spConfig.setInitialState("attribute<?php echo $_attribute->getAttributeId() ?>")
<?php endforeach; ?>
You've to add some code to change the price while changing the drop-down, that file path is www\app\design\frontend\XXX\XXX\Magento_Swatches\web\js, after adding, please do setup upgrade and content-deploy
Step-1: Add Following code in _EventListener: function () {
Best Answer
After that run following command to flush cache,
And see the Magic.....!