1

Thanks in advance for any kind of help in this problem.| I have requirements where I need to display radio button instead of drop down for configurable products. To achieve this i changes the configurable.phtml file (in my custom module). here is the updated code;

<?php $_product    = $this->getProduct();
$_attributes =Mage::helper('core')->decorateArray($this->getAllowAttributes());
 if ($_product->isSaleable() && count($_attributes)):?>
<dl>
<?php foreach($_attributes as $_attribute): ?>
    <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>

    <div class="input-box">
        <?php $configs = $this->getRegularConfig($_attribute->getData('attribute_id')) ?>
        <?php //foreach($configs['attributes'] as $config):?>
            <?php foreach($configs['options'] as $value):?>
                <dd class="<?php if ($_attribute->decoratedIsLast){?>last<?php }?> <?php echo $this->getCssClass($_attribute->getLabel(), $value['label']); ?>">
                <label class="label-radio-configurable" id="<?php echo (float)$value['price'] + (float) $_product->getPrice();?>">
                <input type="radio" name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]"
                        id="attribute<?php echo $_attribute->getAttributeId() ?>"
                        class="validate-custom-configurable"
                        value="<?php echo $value['id']?>"/>
                    <?php echo $value['label']?><!-- (+ $<?php printf("%.2f", $value['price'])?>)-->
                </label>
            <?php endforeach;?>
        <?php //endforeach;?>
         </dd>
    </div>
    <p class="fclear"></p>
<?php endforeach; ?>
</dl>
<?php endif;?>
<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
    document.observe("dom:loaded", function() {
        var customValidation = false;
        var priceFormat = <?php echo $this->helper('tax')->getPriceFormat(); ?>;
        $$('.validate-custom-configurable').each(function(el){
            el.observe('click',function(event){
                // get the highest price
                var $price = el.parentNode.id;
                jQuery('.validate-custom-configurable:checked').each(function(){
                    $price = ($price > jQuery(this).parent().attr('id')) ? $price : jQuery(this).parent().attr('id');
                });
                $$('span .price').each(function(elmn){
                    elmn.update(formatCurrency(parseFloat($price), priceFormat)).innerHTML;
                });
            });
        });
        Validation.addAllThese([
            ['validate-custom-configurable', 'This is a required field.', function(v) {
                $$('.validate-custom-configurable').each(function(el){
                    if (el.checked) {
                        customValidation = true;
                    }
                });
                return customValidation;
            }]
        ]);
    });
</script>

And the getRegularConfig() function looks like this

public function getRegularConfig($attribute_id=0)
{
    $attributes = array();
    $options = array();
    $store = Mage::app()->getStore();
    foreach ($this->getAllowProducts() as $product) {
        $productId  = $product->getId();

        foreach ($this->getAllowAttributes() as $attribute) {
            $productAttribute = $attribute->getProductAttribute();
            $attributeValue = $product->getData($productAttribute->getAttributeCode());
            if (!isset($options[$productAttribute->getId()])) {
                $options[$productAttribute->getId()] = array();
            }

            if (!isset($options[$productAttribute->getId()][$attributeValue])) {
                $options[$productAttribute->getId()][$attributeValue] = array();
            }
            $options[$productAttribute->getId()][$attributeValue][] = $productId;
        }
    }

    $this->_resPrices = array(
        $this->_preparePrice($this->getProduct()->getFinalPrice())
    );

    foreach ($this->getAllowAttributes() as $attribute) {
        $productAttribute = $attribute->getProductAttribute();
        $attributeId = $productAttribute->getId();
        $info = array(
           'id'        => $productAttribute->getId(),
           'code'      => $productAttribute->getAttributeCode(),
           'label'     => $attribute->getLabel(),
           'options'   => array()
        );

        $optionPrices = array();
        $prices = $attribute->getPrices();
        if (is_array($prices)) {
            foreach ($prices as $value) {
                if(!$this->_validateAttributeValue($attributeId, $value, $options)) {
                    continue;
                }

                $info['options'][] = array(
                    'id'    => $value['value_index'],
                    'label' => $value['label'],
                    'price' => $this->_preparePrice($value['pricing_value'], $value['is_percent']),
                    'products'   => isset($options[$attributeId][$value['value_index']]) ? $options[$attributeId][$value['value_index']] : array(),
                );
                $optionPrices[] = $this->_preparePrice($value['pricing_value'], $value['is_percent']);
                //$this->_registerAdditionalJsPrice($value['pricing_value'], $value['is_percent']);
            }
        }
        /**
         * Prepare formated values for options choose
         */
        foreach ($optionPrices as $optionPrice) {
            foreach ($optionPrices as $additional) {
                $this->_preparePrice(abs($additional-$optionPrice));
            }
        }
        if($this->_validateAttributeInfo($info)) {
           $attributes[$attributeId] = $info;
        }
    }
    /*echo '<pre>';
    print_r($this->_prices);
    echo '</pre>';die();*/

    $_request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false);
    $_request->setProductClassId($this->getProduct()->getTaxClassId());
    $defaultTax = Mage::getSingleton('tax/calculation')->getRate($_request);

    $_request = Mage::getSingleton('tax/calculation')->getRateRequest();
    $_request->setProductClassId($this->getProduct()->getTaxClassId());
    $currentTax = Mage::getSingleton('tax/calculation')->getRate($_request);

    $taxConfig = array(
        'includeTax'        => Mage::helper('tax')->priceIncludesTax(),
        'showIncludeTax'    => Mage::helper('tax')->displayPriceIncludingTax(),
        'showBothPrices'    => Mage::helper('tax')->displayBothPrices(),
        'defaultTax'        => $defaultTax,
        'currentTax'        => $currentTax,
        'inclTaxTitle'      => Mage::helper('catalog')->__('Incl. Tax'),
    );

    $config = array(
        'attributes'        => $attributes,
        'template'          => str_replace('%s', '#{price}', $store->getCurrentCurrency()->getOutputFormat()),
        'basePrice'         => $this->_registerJsPrice($this->_convertPrice($this->getProduct()->getFinalPrice())),
        'oldPrice'          => $this->_registerJsPrice($this->_convertPrice($this->getProduct()->getPrice())),
        'productId'         => $this->getProduct()->getId(),
        'chooseText'        => Mage::helper('catalog')->__('Choose an Option...'),
        'taxConfig'         => $taxConfig,
    );

    if($attribute_id && isset($config['attributes'][$attribute_id])){
        return $config['attributes'][$attribute_id];
    }

    return $config;
}

Now what i want is to be able to disable the options values (radio buttons) that are not available for current selection as magento do by default. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.