天天看点

How to display custom select option on product list page?

here's the code"

add it to your list.phtml and chance your stylesheet

<?php if($_product->isSaleable()): ?>

    <form action="<?php echo Mage::getBaseUrl(); ?>checkout/cart/add/” method="post" id="product_addtocart_form_<?php echo $_product->getId() ?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>

        <fieldset class="no-display">

            <input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />

            <input type="hidden" name="related_product" id="related-products-field" value="" />

        </fieldset>

<?php // Product options ?>

    <?php $product = Mage::getModel('catalog/product'); ?>

    <?php $product->load($_product->getId()); ?>

<?php // Configurable products ?>

    <?php if($product->getTypeId() == "configurable"): ?>

        <?php $attValConfig = $product->getTypeInstance()->getConfigurableAttributesAsArray(); ?>

        <?php if(sizeof($attValConfig)): ?>

            <?php foreach($attValConfig as $attValConfigSingle): ?>

                <fieldset class="product-options" id="product-options-wrapper">

                    <label><?php echo $attValConfigSingle['label']; ?>:</label>

                    <select name="super_attribute[<?php echo $attValConfigSingle['attribute_id'] ?>]" id="attribute<?php echo $_product->getId() ?>" class="required-entry super-attribute-select">

                        <?php foreach($attValConfigSingle['values'] as $attValConfigSingleVal): ?>

                            <option value="<?php echo $attValConfigSingleVal['value_index'] ?>"><?php echo $attValConfigSingleVal['label'] ?></option>

                        <?php endforeach; ?>

                    </select>

                </fieldset>

            <?php endforeach; ?>

        <?php endif; ?>

    <?php endif; ?>

<?php // Custom options  - tested but not fully implemented ?>

    <?php

    $xml = "";

    $attVal = $product->getOptions();

    if(sizeof($attVal)) {

      foreach($attVal as $optionVal) {

        $xml .= $optionVal->getTitle().": ";

        $xml .= "<select name='options[".$optionVal->getId()."]'>";

        foreach($optionVal->getValues() as $valuesKey => $valuesVal) {

          $xml .= "<option value='".$valuesVal->getId()."'>".$valuesVal->getTitle()."</option>";

        }

        $xml .= "</select>";

      }

    }

    echo $xml;

    ?>

<?php // Product quantity and add  ?>

    <?php if(!$_product->isGrouped()): ?>                                

        <label>Qty:</label>

        <input name="qty" type="text" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product)==null?1:$this->getMinimalQty($_product) ?>"/>

        <button type="button" οnclick="productAddToCartForm<?php echo $_product->getId() ?>.submit()">Add to Cart</button>

    <?php endif; ?>

    </form>

    <script type="text/javascript">

            var productAddToCartForm<?php echo $_product->getId() ?> = new VarienForm('product_addtocart_form_<?php echo $_product->getId() ?>');

            productAddToCartForm<?php echo $_product->getId() ?>.submit = function(){

                    if (this.validator.validate()) {

                            this.form.submit();

                    }

            }.bind(productAddToCartForm<?php echo $_product->getId() ?>);

    </script>

<?php else: ?>

    <div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>

<?php endif; ?>