Set default value on configurable drop down in magento

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Set default value on configurable drop down in magento



I have a Configurable Product with 3 Options - here is what the drop down menu looks like on the product page.


Bundle Deals

* Required Fields
Choose an Option...
- Single Product £10
- 5 Product Bundle £50
- 10 Product Bundle £100



Default value on page load is £10.00 but if I hit add to cart it flags up with - * Required Fields & user is prompted to select an option from the drop down.


£10.00


* Required Fields



By default I would like the drop down menu to load with - Single Product £10 as the default value.


Single Product £10



Hope that all makes sense? I can't find this functionality in the Magento version 1.9 CE, which I am using



FINAL EDIT >> thanks to everyone for helping - got a fix - Very glad! visit link.. similar to a suggestion here but something in the code seemed to work for me



http://iamvikram.com/magento-remove-choose-an-option-from-configurable-products-dropdown/


http://iamvikram.com/magento-remove-choose-an-option-from-configurable-products-dropdown/



A thank you msg has been mailed :)





Might have some luck on magento.stackexchange.com
– Andy
Jul 24 '14 at 12:18





glad that you have it sorted out. I also like to remove the Choose an Option from the drop down cause it's useless if the option is required.
– William Tran
Jul 24 '14 at 23:25




4 Answers
4



Copy below file in your local


/app/design/frontend/default/mytheme/template/catalog/product/view/type/options/configurable.phtml


<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>



It shows 'Choose an Option...' as a default value


Choose an Option...



You can change it like below to simply select the first real element in your select:


$$('#attribute525 option')[1].selected = true;



Check with your attributeid. The above is just an example.





Thanks @Slimshadddyyy - Cant get this to work.. If I change the text from "Choose an Option..." to "Choose a Product..." Still the old msg Choose an Option comes up in the drop box.. I have refreshed cache & indexes.. Maybe a different file is actioning the function.. ? I added the '$$('#attribute525 option')[1].selected = true;' - still lookin at this
– dan_code89
Jul 24 '14 at 13:34





@dan_code89 on your product page, inspect the dropdown and check the attributeid of select. For me its attribute502. For you it could be something else. Pass that attributeid
– Slimshadddyyy
Jul 25 '14 at 4:11


select


attribute502


attributeid



Navigate to Catalog->Attributes->Manage Attributes in your magento admin and search for your attribute.


Catalog->Attributes->Manage Attributes



Click to edit and select No to Values required.


No


Values required.



Hope it will work.



JQuery to default select first value is -


jQuery('#attribute135>option:eq(1)').attr('selected', true);





we need to select the first option as default selected and not to by pass validation
– Slimshadddyyy
Jul 24 '14 at 12:25






Alright, so you are having 3 attributes and you want to select first option as default selected for all 3 attributes ?
– TBI
Jul 24 '14 at 12:29





Thanks SlimShadddyyy - I tried that - I still get flagged for Required fields.
– dan_code89
Jul 24 '14 at 12:30





What if you just remove the Please Select message from Custom Option Dropdown on Magento Product View and making the first option as default selected ?
– Slimshadddyyy
Jul 24 '14 at 12:32



Please Select





Yeah - If it can select the first attribute as default I would be very happy :) Or if I can turn off the validation (Required Fields) that would also be great - But I think configureable products are always Required..
– dan_code89
Jul 24 '14 at 12:32



This could get tricky if you have 2+ options (it's confusing but what you describe above is 1 option with 3 selection), says Color and Size. Assuming Color is the first Option, Size's selection will be updated once Color is selected.



For example, you have a shirt available in Red in size (S,L) and Blue in size (M,L), once you select Blue, Magento observe the event 'onChange' of the Color option and update the Size option to M, L.



This is what I would do to get it done correctly in PrototypeJS:


<script type='text/javascript>
var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);

document.observe('dom:loaded', function() {
var el = $('attribute<?php echo $_attribute->getAttributeId() ?>');
el.selectedIndex = 1;
el[0].remove();
if ("createEvent" in document)
var ev = document.createEvent("HTMLEvents");
ev.initEvent("change", false, true);
el.dispatchEvent(ev);
else
el.fireEvent("onchange");

</script>



in configurable.phtml, please note the fireEvent / dispatchEvent.
el[0].remove will get rid of "Select an option.."


configurable.phtml



This is the least intrusive method that I know of





Have tried this @William Tran - sorry I couldnt get it working.. error msgs... Can you explain it a bit more & the fireEvent?. I am not a JavaScript writer.. Just HTML & CSS are my main thing.. Would be Much Obliged for a solution here :/
– dan_code89
Jul 24 '14 at 17:23






can you let me know what kind of error message?
– William Tran
Jul 24 '14 at 22:28





My answer is for configurable product with one option: you have to populate $_attribute before getting to the script.
– William Tran
Jul 25 '14 at 1:16



Open appdesignfrontend[your package][your theme]templatecatalogproductviewtypeoptionsconfigurable.phtml



Now add the below java-script code after :-


var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);


function fireEvent(element,event)

if (document.createEventObject)

// dispatch for IE

var evt = document.createEventObject();

return element.fireEvent('on'+event,evt)



else

// dispatch for firefox + others

var evt = document.createEvent("HTMLEvents");

evt.initEvent(event, true, true ); // event type,bubbling,cancelable

return !element.dispatchEvent(evt);





Event.observe(window, 'load', function()

spConfig.settings[0].selectedIndex = 1;

obj = spConfig.settings[0]; // this grabs the first select item

Event.observe(obj,'change',function());

fireEvent(obj,'change'); // this simulates selecting the first option, which triggers

spConfig.settings[1].selectedIndex = 1; // this selects the first option of the second attribute drop menu
);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered