If you want to do the same as below with simple products, I made a simple piece of code to make it work easily and smoothly with jQuery. This is particularly useful on small projects where products don’t need to be an individual product. Inmy use case there weren’t any other product like this one and SKU wasn’t used.
Steps |
Add as many color as you want and the SKU is consider as a color HEX.
- Place at the end of your file the code below in:
app/design/frontend/default/default/template/catalog/product/view/options/type/select.phtml
- <?php if($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN):?>
- <dt><label>Select your color</label></dt>
- <ul>
- <?php foreach ($_option->getValues() as $_value) { ?>
- <li class="colorpick <?php echo str_replace(" ","_",$_value->getid()) ?>" style="background-color:#<?php echo $_value->getSku(); ?>;"> </li>
- <?php } ?>
- </ul>
- <script src="http://code.jquery.com/jquery-latest.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $(".colorpick").click(function() {
- var id = this.className.split(' ')[1];
- $("select[name='options[<?php echo $_option->getid() ?>]']").val(id);
- if($(".colorpick").hasClass('highlight')) {
- $(".colorpick").removeClass('highlight');
- }
- $(this).addClass('highlight');
- });
- $("select[name='options[<?php echo $_option->getid() ?>]']").change(function() {
- var selected_option = $("select[name='options[<?php echo $_option->getid() ?>]']").val();
- if($(".colorpick").hasClass('highlight')) {
- $(".colorpick").removeClass('highlight');
- }
- $("."+selected_option).addClass('highlight');
- });
- });
- </script>
- <?php endif; ?>
- add a some CSS to stylish your color boxes:
- .highlight { border: 2px solid #000; }
- .colorpick { cursor:pointer; width:20px; height:20px; float:left; margin:1px; display:inline; vertical-al