Magento 2: How to set default option in configurable options?How to set default value in custom option in magento2Set default configurable selection in Magento 2.1Magento 2: How to set minimum amount option by default in product detail page for configurable options?How to set default option on configurable product dropdown attribute?Multi-select select option value issue Optionshow to set default color configurable productMagento2 override admin js fileSet default configurable selection in Magento 2.1How to make options default for multiselect attribute in Magento 2How to remove bundle options from product when loading the admin edit view?Default Custom Options by CategoryIf a configurable product has only one option available, should it automatically select it?Set first configurable product as default image
How do I gain back my faith in my PhD degree?
Should I tell management that I intend to leave due to bad software development practices?
ssTTsSTtRrriinInnnnNNNIiinngg
Can a virus destroy the BIOS of a modern computer?
How does a predictive coding aid in lossless compression?
How badly should I try to prevent a user from XSSing themselves?
Can we compute the area of a quadrilateral with one right angle when we only know the lengths of any three sides?
Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?
What about the virus in 12 Monkeys?
Is it acceptable for a professor to tell male students to not think that they are smarter than female students?
How do I handle a potential work/personal life conflict as the manager of one of my friends?
Why would the Red Woman birth a shadow if she worshipped the Lord of the Light?
How could indestructible materials be used in power generation?
Is "remove commented out code" correct English?
How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)
How do I deal with an unproductive colleague in a small company?
Is there a hemisphere-neutral way of specifying a season?
Is it logically or scientifically possible to artificially send energy to the body?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
Unlock My Phone! February 2018
Detention in 1997
What does “the session was packed” mean in this context?
What exploit Are these user agents trying to use?
Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?
Magento 2: How to set default option in configurable options?
How to set default value in custom option in magento2Set default configurable selection in Magento 2.1Magento 2: How to set minimum amount option by default in product detail page for configurable options?How to set default option on configurable product dropdown attribute?Multi-select select option value issue Optionshow to set default color configurable productMagento2 override admin js fileSet default configurable selection in Magento 2.1How to make options default for multiselect attribute in Magento 2How to remove bundle options from product when loading the admin edit view?Default Custom Options by CategoryIf a configurable product has only one option available, should it automatically select it?Set first configurable product as default image
How to set drops downs on configurable products to be set to the first option rather than please select.
I have tried below code:
<script>
require(['jquery', 'jquery/ui'], function($)
$('.product-add-form .field select').each(function(i, obj)
$(obj).find('option:eq(1)').prop('selected', true);
);
);
</script>
However, this fails unless I time it out a bit which feels messy. It also does not update price when using this method.
magento2 product multiselect-attribute
add a comment |
How to set drops downs on configurable products to be set to the first option rather than please select.
I have tried below code:
<script>
require(['jquery', 'jquery/ui'], function($)
$('.product-add-form .field select').each(function(i, obj)
$(obj).find('option:eq(1)').prop('selected', true);
);
);
</script>
However, this fails unless I time it out a bit which feels messy. It also does not update price when using this method.
magento2 product multiselect-attribute
You can try Rakesh's answer described below link. Hope it helps. magento.stackexchange.com/questions/142242/…
– Abhishek Panchal
Aug 14 '17 at 14:29
This is for swatches i have looked into this.
– harri
Aug 14 '17 at 14:29
add a comment |
How to set drops downs on configurable products to be set to the first option rather than please select.
I have tried below code:
<script>
require(['jquery', 'jquery/ui'], function($)
$('.product-add-form .field select').each(function(i, obj)
$(obj).find('option:eq(1)').prop('selected', true);
);
);
</script>
However, this fails unless I time it out a bit which feels messy. It also does not update price when using this method.
magento2 product multiselect-attribute
How to set drops downs on configurable products to be set to the first option rather than please select.
I have tried below code:
<script>
require(['jquery', 'jquery/ui'], function($)
$('.product-add-form .field select').each(function(i, obj)
$(obj).find('option:eq(1)').prop('selected', true);
);
);
</script>
However, this fails unless I time it out a bit which feels messy. It also does not update price when using this method.
magento2 product multiselect-attribute
magento2 product multiselect-attribute
edited Jan 24 '18 at 9:24
harri
asked Aug 14 '17 at 14:22
harriharri
3,0411656
3,0411656
You can try Rakesh's answer described below link. Hope it helps. magento.stackexchange.com/questions/142242/…
– Abhishek Panchal
Aug 14 '17 at 14:29
This is for swatches i have looked into this.
– harri
Aug 14 '17 at 14:29
add a comment |
You can try Rakesh's answer described below link. Hope it helps. magento.stackexchange.com/questions/142242/…
– Abhishek Panchal
Aug 14 '17 at 14:29
This is for swatches i have looked into this.
– harri
Aug 14 '17 at 14:29
You can try Rakesh's answer described below link. Hope it helps. magento.stackexchange.com/questions/142242/…
– Abhishek Panchal
Aug 14 '17 at 14:29
You can try Rakesh's answer described below link. Hope it helps. magento.stackexchange.com/questions/142242/…
– Abhishek Panchal
Aug 14 '17 at 14:29
This is for swatches i have looked into this.
– harri
Aug 14 '17 at 14:29
This is for swatches i have looked into this.
– harri
Aug 14 '17 at 14:29
add a comment |
2 Answers
2
active
oldest
votes
Override configurable.js within the Configurable Products Module. The following code can be placed at around line 370 of configurable.js:
if (i == 0)
this.options.values[attributeId] = options[i].id;
This is placed at the end of options loop so that the default option is set just after the first option is created for each attribute / select.
In the case that options were set in url the code for updating defaults was needed to run again to set the values correctly which can be achieved by adding below just outside of the loop:
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
This is placed just after the for loop for options so that it's run just once at the end of the option creation.
So _fillSelect within configurable.js now should look like below:
_fillSelect: function (element)
var attributeId = element.id.replace(/[a-z]*/, ''),
options = this._getAttributeOptions(attributeId),
prevConfig,
index = 1,
allowedProducts,
i,
j;
this._clearSelect(element);
element.options[0] = new Option('', '');
element.options[0].innerHTML = this.options.spConfig.chooseText;
prevConfig = false;
if (element.prevSetting)
prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
if (options)
for (i = 0; i < options.length; i++)
allowedProducts = [];
if (prevConfig)
for (j = 0; j < options[i].products.length; j++)
// prevConfig.config can be undefined
if (prevConfig.config &&
prevConfig.config.allowedProducts &&
prevConfig.config.allowedProducts.indexOf(options[i].products[j]) > -1)
allowedProducts.push(options[i].products[j]);
else
allowedProducts = options[i].products.slice(0);
if (allowedProducts.length > 0)
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this._getOptionLabel(options[i]), options[i].id);
if (typeof options[i].price !== 'undefined')
element.options[index].setAttribute('price', options[i].prices);
element.options[index].config = options[i];
index++;
//new code here - updates the value to first option
if (i == 0)
this.options.values[attributeId] = options[i].id;
//new code here checks if configurations are set in url and resets them if needed
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
,
Original file:
vendor/magento/module-configurable-product/view/frontend/web/js/configurable.js
Location when overridden in theme:
app/design/frontend/ThemeVendor/Theme/Magento_ConfigurableProduct/web/js/configurable.js
add a comment |
Override vendormagentomodule-swatchesviewfrontendwebjsswatch-renderer.js
to your theme
your theme file location will be:
app/design/frontend/Vendor/themename/Magento_Swatches/web/js/swatch-renderer.js
change _RenderControls() function
just wrinte below code at the end of this function
var swatchLength = $('.swatch-attribute').length;
if(swatchLength >= 1)
if($('.swatch-attribute').hasClass("size"))
$('.swatch-option').first().trigger('click');
http://prntscr.com/n6qo62
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f189088%2fmagento-2-how-to-set-default-option-in-configurable-options%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Override configurable.js within the Configurable Products Module. The following code can be placed at around line 370 of configurable.js:
if (i == 0)
this.options.values[attributeId] = options[i].id;
This is placed at the end of options loop so that the default option is set just after the first option is created for each attribute / select.
In the case that options were set in url the code for updating defaults was needed to run again to set the values correctly which can be achieved by adding below just outside of the loop:
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
This is placed just after the for loop for options so that it's run just once at the end of the option creation.
So _fillSelect within configurable.js now should look like below:
_fillSelect: function (element)
var attributeId = element.id.replace(/[a-z]*/, ''),
options = this._getAttributeOptions(attributeId),
prevConfig,
index = 1,
allowedProducts,
i,
j;
this._clearSelect(element);
element.options[0] = new Option('', '');
element.options[0].innerHTML = this.options.spConfig.chooseText;
prevConfig = false;
if (element.prevSetting)
prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
if (options)
for (i = 0; i < options.length; i++)
allowedProducts = [];
if (prevConfig)
for (j = 0; j < options[i].products.length; j++)
// prevConfig.config can be undefined
if (prevConfig.config &&
prevConfig.config.allowedProducts &&
prevConfig.config.allowedProducts.indexOf(options[i].products[j]) > -1)
allowedProducts.push(options[i].products[j]);
else
allowedProducts = options[i].products.slice(0);
if (allowedProducts.length > 0)
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this._getOptionLabel(options[i]), options[i].id);
if (typeof options[i].price !== 'undefined')
element.options[index].setAttribute('price', options[i].prices);
element.options[index].config = options[i];
index++;
//new code here - updates the value to first option
if (i == 0)
this.options.values[attributeId] = options[i].id;
//new code here checks if configurations are set in url and resets them if needed
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
,
Original file:
vendor/magento/module-configurable-product/view/frontend/web/js/configurable.js
Location when overridden in theme:
app/design/frontend/ThemeVendor/Theme/Magento_ConfigurableProduct/web/js/configurable.js
add a comment |
Override configurable.js within the Configurable Products Module. The following code can be placed at around line 370 of configurable.js:
if (i == 0)
this.options.values[attributeId] = options[i].id;
This is placed at the end of options loop so that the default option is set just after the first option is created for each attribute / select.
In the case that options were set in url the code for updating defaults was needed to run again to set the values correctly which can be achieved by adding below just outside of the loop:
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
This is placed just after the for loop for options so that it's run just once at the end of the option creation.
So _fillSelect within configurable.js now should look like below:
_fillSelect: function (element)
var attributeId = element.id.replace(/[a-z]*/, ''),
options = this._getAttributeOptions(attributeId),
prevConfig,
index = 1,
allowedProducts,
i,
j;
this._clearSelect(element);
element.options[0] = new Option('', '');
element.options[0].innerHTML = this.options.spConfig.chooseText;
prevConfig = false;
if (element.prevSetting)
prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
if (options)
for (i = 0; i < options.length; i++)
allowedProducts = [];
if (prevConfig)
for (j = 0; j < options[i].products.length; j++)
// prevConfig.config can be undefined
if (prevConfig.config &&
prevConfig.config.allowedProducts &&
prevConfig.config.allowedProducts.indexOf(options[i].products[j]) > -1)
allowedProducts.push(options[i].products[j]);
else
allowedProducts = options[i].products.slice(0);
if (allowedProducts.length > 0)
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this._getOptionLabel(options[i]), options[i].id);
if (typeof options[i].price !== 'undefined')
element.options[index].setAttribute('price', options[i].prices);
element.options[index].config = options[i];
index++;
//new code here - updates the value to first option
if (i == 0)
this.options.values[attributeId] = options[i].id;
//new code here checks if configurations are set in url and resets them if needed
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
,
Original file:
vendor/magento/module-configurable-product/view/frontend/web/js/configurable.js
Location when overridden in theme:
app/design/frontend/ThemeVendor/Theme/Magento_ConfigurableProduct/web/js/configurable.js
add a comment |
Override configurable.js within the Configurable Products Module. The following code can be placed at around line 370 of configurable.js:
if (i == 0)
this.options.values[attributeId] = options[i].id;
This is placed at the end of options loop so that the default option is set just after the first option is created for each attribute / select.
In the case that options were set in url the code for updating defaults was needed to run again to set the values correctly which can be achieved by adding below just outside of the loop:
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
This is placed just after the for loop for options so that it's run just once at the end of the option creation.
So _fillSelect within configurable.js now should look like below:
_fillSelect: function (element)
var attributeId = element.id.replace(/[a-z]*/, ''),
options = this._getAttributeOptions(attributeId),
prevConfig,
index = 1,
allowedProducts,
i,
j;
this._clearSelect(element);
element.options[0] = new Option('', '');
element.options[0].innerHTML = this.options.spConfig.chooseText;
prevConfig = false;
if (element.prevSetting)
prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
if (options)
for (i = 0; i < options.length; i++)
allowedProducts = [];
if (prevConfig)
for (j = 0; j < options[i].products.length; j++)
// prevConfig.config can be undefined
if (prevConfig.config &&
prevConfig.config.allowedProducts &&
prevConfig.config.allowedProducts.indexOf(options[i].products[j]) > -1)
allowedProducts.push(options[i].products[j]);
else
allowedProducts = options[i].products.slice(0);
if (allowedProducts.length > 0)
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this._getOptionLabel(options[i]), options[i].id);
if (typeof options[i].price !== 'undefined')
element.options[index].setAttribute('price', options[i].prices);
element.options[index].config = options[i];
index++;
//new code here - updates the value to first option
if (i == 0)
this.options.values[attributeId] = options[i].id;
//new code here checks if configurations are set in url and resets them if needed
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
,
Original file:
vendor/magento/module-configurable-product/view/frontend/web/js/configurable.js
Location when overridden in theme:
app/design/frontend/ThemeVendor/Theme/Magento_ConfigurableProduct/web/js/configurable.js
Override configurable.js within the Configurable Products Module. The following code can be placed at around line 370 of configurable.js:
if (i == 0)
this.options.values[attributeId] = options[i].id;
This is placed at the end of options loop so that the default option is set just after the first option is created for each attribute / select.
In the case that options were set in url the code for updating defaults was needed to run again to set the values correctly which can be achieved by adding below just outside of the loop:
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
This is placed just after the for loop for options so that it's run just once at the end of the option creation.
So _fillSelect within configurable.js now should look like below:
_fillSelect: function (element)
var attributeId = element.id.replace(/[a-z]*/, ''),
options = this._getAttributeOptions(attributeId),
prevConfig,
index = 1,
allowedProducts,
i,
j;
this._clearSelect(element);
element.options[0] = new Option('', '');
element.options[0].innerHTML = this.options.spConfig.chooseText;
prevConfig = false;
if (element.prevSetting)
prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
if (options)
for (i = 0; i < options.length; i++)
allowedProducts = [];
if (prevConfig)
for (j = 0; j < options[i].products.length; j++)
// prevConfig.config can be undefined
if (prevConfig.config &&
prevConfig.config.allowedProducts &&
prevConfig.config.allowedProducts.indexOf(options[i].products[j]) > -1)
allowedProducts.push(options[i].products[j]);
else
allowedProducts = options[i].products.slice(0);
if (allowedProducts.length > 0)
options[i].allowedProducts = allowedProducts;
element.options[index] = new Option(this._getOptionLabel(options[i]), options[i].id);
if (typeof options[i].price !== 'undefined')
element.options[index].setAttribute('price', options[i].prices);
element.options[index].config = options[i];
index++;
//new code here - updates the value to first option
if (i == 0)
this.options.values[attributeId] = options[i].id;
//new code here checks if configurations are set in url and resets them if needed
if (window.location.href.indexOf('#') !== -1) this._parseQueryParams(window.location.href.substr(window.location.href.indexOf('#') + 1));
,
Original file:
vendor/magento/module-configurable-product/view/frontend/web/js/configurable.js
Location when overridden in theme:
app/design/frontend/ThemeVendor/Theme/Magento_ConfigurableProduct/web/js/configurable.js
edited Aug 8 '18 at 18:59
answered Aug 14 '17 at 15:07
harriharri
3,0411656
3,0411656
add a comment |
add a comment |
Override vendormagentomodule-swatchesviewfrontendwebjsswatch-renderer.js
to your theme
your theme file location will be:
app/design/frontend/Vendor/themename/Magento_Swatches/web/js/swatch-renderer.js
change _RenderControls() function
just wrinte below code at the end of this function
var swatchLength = $('.swatch-attribute').length;
if(swatchLength >= 1)
if($('.swatch-attribute').hasClass("size"))
$('.swatch-option').first().trigger('click');
http://prntscr.com/n6qo62
add a comment |
Override vendormagentomodule-swatchesviewfrontendwebjsswatch-renderer.js
to your theme
your theme file location will be:
app/design/frontend/Vendor/themename/Magento_Swatches/web/js/swatch-renderer.js
change _RenderControls() function
just wrinte below code at the end of this function
var swatchLength = $('.swatch-attribute').length;
if(swatchLength >= 1)
if($('.swatch-attribute').hasClass("size"))
$('.swatch-option').first().trigger('click');
http://prntscr.com/n6qo62
add a comment |
Override vendormagentomodule-swatchesviewfrontendwebjsswatch-renderer.js
to your theme
your theme file location will be:
app/design/frontend/Vendor/themename/Magento_Swatches/web/js/swatch-renderer.js
change _RenderControls() function
just wrinte below code at the end of this function
var swatchLength = $('.swatch-attribute').length;
if(swatchLength >= 1)
if($('.swatch-attribute').hasClass("size"))
$('.swatch-option').first().trigger('click');
http://prntscr.com/n6qo62
Override vendormagentomodule-swatchesviewfrontendwebjsswatch-renderer.js
to your theme
your theme file location will be:
app/design/frontend/Vendor/themename/Magento_Swatches/web/js/swatch-renderer.js
change _RenderControls() function
just wrinte below code at the end of this function
var swatchLength = $('.swatch-attribute').length;
if(swatchLength >= 1)
if($('.swatch-attribute').hasClass("size"))
$('.swatch-option').first().trigger('click');
http://prntscr.com/n6qo62
answered 22 hours ago
Jigs ParmarJigs Parmar
1,132423
1,132423
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f189088%2fmagento-2-how-to-set-default-option-in-configurable-options%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can try Rakesh's answer described below link. Hope it helps. magento.stackexchange.com/questions/142242/…
– Abhishek Panchal
Aug 14 '17 at 14:29
This is for swatches i have looked into this.
– harri
Aug 14 '17 at 14:29