user3738149
user3738149

Reputation: 63

magento output custom options

Is it possible to output the available list of custom options onto the view.phtml page within Magento? Obviously the drop down lists allow you to filter down but I need to list them in a html table and allow the customer to add to cart on each item.

Upvotes: 1

Views: 1099

Answers (1)

Rajeev K Tomy
Rajeev K Tomy

Reputation: 2214

I will show you how to convert dropdown and multi-select options into table format. This is the code that do the trick.

<?php
$product = Mage::Registry('current_product');
$options = $product->getOptions();
//check for option exists
if($options):
    foreach ($options as $option) :
        //table format allows only for select options
        if($option->getType()== Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN || 
            $option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE):

            $require = ($option->getIsRequire()) ? ' required-entry' : '';
            if($option->getType()== Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN):
                    $value_name = 'options['.$option->getid().']';
            else:
                $value_name = 'options['.$option->getid().'][]';
            endif;
?>
            <table border="1" style="width:500px;margin-bottom:30px">
                <thead>
                    <tr<?php if ($option->getIsRequire()) echo ' class="required"' ?>>
                        <th><?php echo $option->getTitle() ?></th>
                        <th><?php if ($option->getIsRequire()) echo '<em>*</em>' ?></th>
                    </tr>
                </thead>
                <tbody id="<?php echo 'select_'.$option->getId() ?>" class="<?php echo $require.' product-custom-option' ?>">       
            <?php
                foreach ($option->getValues() as  $values) :
                    $value = $values->getOptionTypeId();
                    $value_title = $values->getTitle();
            ?>
                    <tr>
                        <td>
                            <?php
                                if($option->getType()== Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN):
                            ?>
                                    <input type="radio" name="<?php echo $value_name; ?>" class="radio_option" id="<?php echo 'radio_option_'.$value ?>" value="<?php echo $value; ?>" />
                            <?php
                                else:
                            ?>
                                    <input type="checkbox" name="<?php echo $value_name; ?>" class="checkbox_option" id="<?php echo 'checkbox_option_'.$value ?>" value="<?php echo $value; ?>" />
                            <?php
                                endif;
                            ?>
                        </td>
                        <td><?php echo $value_title; ?></td>
                    </tr>
            <?php
                endforeach;
            ?>
                </tbody>
            </table>
<?php
        endif;  
    endforeach;
endif;
?>

Here for dropdown options, we are using radio button to show their values. For multiselect options, checkbox inputs are used. So if checkbox appears in the table, that means it a multi select option and hence more than one checkboxes can have tick at a time. Since radio buttons are used for dropdown options, it is important to make sure that, only one radio button is checked at a time. (We need to use javascript for ensure this).

So if we are set some custom options for a product through admin like this..

Admin Custom Options

The output correspond to our code will be look like this

enter image description here

Note: Please note that, the code is in very basic format. You may require additional css and javascripts make it more user frontly. This is for giving you a basic idea and additional css, js are hence out of box topic.

For more information about code, check out my blog

Hope it helps

Upvotes: 1

Related Questions