100% Satisfaction Guaranteed. Click here to read why you should by from us.

Magento Configurable Products - Setting Default Value for Options


Have you ever wanted to be able to set the default value for options on configurable products?  We did!  So we decided to build this into our stores.

We use this feature for:

  • Allowing us to default options to the most popular options to save customers those extra few clicks
  • Lets us display search and category listings with all our available color options - just like Zappos!

 


Our Solution

To add this feature to our stores, we developed an extension that we are currently using internally. It adds radio buttons on the product edit page beside configurable options, allowing you to specify a default value.  Then on the frontend, there is logic to set the correct values in the product option dropdowns.  We were able to do this without any modification to core code.

The default behavior in Magento for configurable products is for the options to default to “Choose an option”.  There is no way to specify a default value to automatically be selected.

This extra feature we added lets us default options to any value we want. Another added benefit is that we can list products like popular online sites like Zappos.  We wanted the ability to list products on category and search grids in all available colors.  To do this, we create a configurable product for each color.  Then for each of the configurable products, we set a different default color. This way, a customer has the ability to see all colors on the product page, and they can still switch to any other color they desire.  Check it out here.

Update August 24 / 2010 - We have made this into an extension
Available Here.

The extension does not modify any core code, is very high quality code, and tested thoroughly.

 

Here are some screenshots:

1. Editing a configurable product, we can set the default value for an option by clicking a radio button:


 


2. When a customer goes to a product, they see this default option selected for them:



An Alternative Javascript Method

If you don’t have many products, there is a way to get the same type of behavior using a javascript “hack”.

This snippet of code, can be placed on a product page and it will select the first value for each configurable option. With a little modification, you can modify it so that it selects a specific option.

  1.  
  2. function fireEvent(element,event)
  3. {
  4. if (document.createEventObject)
  5. {
  6. // dispatch for IE
  7. var evt = document.createEventObject();
  8. return element.fireEvent('on'+event,evt);
  9. }
  10. else
  11. {
  12. // dispatch for firefox + others
  13. var evt = document.createEvent("HTMLEvents");
  14. evt.initEvent(event, true, true ); // event type,bubbling,cancelable
  15. return !element.dispatchEvent(evt);
  16. }
  17. }
  18.  
  19. Event.observe(window, 'load', function() {
  20. var spConfigIndex = 0;
  21. for (spConfigIndex=0; spConfigIndex<spConfig.settings.length; ++spConfigIndex)
  22. {
  23. spConfig.settings[spConfigIndex].selectedIndex = 1;
  24. obj = spConfig.settings[spConfigIndex]; // this grabs the first select item
  25. Event.observe(obj,'change',function(){});
  26. fireEvent(obj,'change'); // this simulates selecting the first option, which triggers
  27. }
  28. });
  29.  

UPDATE - August 11 / 2010

Here is an updated version of the code above.  It adds a small delay in between selecting each configurable option.  This allows just enough time for the next select box to load up.

  1.  
  2. function fireEvent(element,event)
  3. {
  4. if (document.createEventObject)
  5. {
  6. // dispatch for IE
  7. var evt = document.createEventObject();
  8. return element.fireEvent('on'+event,evt);
  9. }
  10. else
  11. {
  12. // dispatch for firefox + others
  13. var evt = document.createEvent("HTMLEvents");
  14. evt.initEvent(event, true, true );
  15. return !element.dispatchEvent(evt);
  16. }
  17. }
  18.  
  19. function setDefaultConfigOptions()
  20. {
  21. if (spConfigIndex >= spConfig.settings.length)
  22. {
  23. return; // stop
  24. }
  25.  
  26. spConfig.settings[spConfigIndex].selectedIndex = 1;
  27. var obj = spConfig.settings[spConfigIndex];
  28.  
  29. ++spConfigIndex;
  30.  
  31. Event.observe(obj,'change',function(){});
  32. fireEvent(obj,'change');
  33.  
  34. window.setTimeout("setDefaultConfigOptions()", 1); // Add a small delay before moving onto the next option
  35. }
  36.  
  37. var spConfigIndex = 0;
  38. Event.observe(window, 'load', function() {
  39. setDefaultConfigOptions();
  40. });
  41.  

The javascript hack above would have been too time consuming for us to use on a live store (too many products and too many options being changed over time).  That is why we decided to add this extra feature to our stores. Our store managers love it.

 




Page 1 of 1 pages