phphtmlmagentoe-commerceshopping-cart

Magento if product configurable show options


I need to show product options on my related products:

In list.phtml for every configurable products i show its options (option means an attribute like colour) with this code:

<?php if($_product->getTypeId() == 'configurable'): ?> 
   <?php echo $this->getOptionsHtml($_product); ?> 
<?php endif; ?>

Result: nothing!!!

What is wrong here? Why in related.phtml is not working?

getOptionsHtml - This function is from Belvg ColorSwatchPro extension.

<?php
class Belvg_ColorSwatchPro_Block_Product_List extends Mage_Catalog_Block_Product_List
{       
    public function getOptionsHtml($_product)
    {    die('sss');
        $block = $this->getLayout()->createBlock(
            'Belvg_ColorSwatchPro_Block_Product_List_Options',
            'product_list_options',
            array('template' => 'colorswatch/product/list/options.phtml'
        ));

        $block->setProduct($_product);

        return $block->toHtml();
    }
}

Solution

  • So now I will try to explain you why it is in this way.

    All templates in magento are assigned to some blocks. For example in your case list.phtml uses Belvg_ColorSwatchPro_Block_Product_List block. So if inside template you call construction $this->getOptionsHtml($_product); it means that you call the method of Belvg_ColorSwatchPro_Block_Product_List block.

    But when you use $this->getOptionsHtml($_product) in related.phtml it doesn't work because this template is assigned to block Mage_Catalog_Block_Product_List_Related which has no method getOptionsHtml.

    To make that workable, I can advise you the easiest way. Inside related.phtml template instead of

    <?php if($_product->getTypeId() == 'configurable'): ?> 
        <?php echo $this->getOptionsHtml($_product); ?> 
    <?php endif; ?>
    

    use

    <?php if($_item->getTypeId() == 'configurable') {
        $block = $this->getLayout()->createBlock(
            'Belvg_ColorSwatchPro_Block_Product_List_Options',
            'product_list_options',
            array('template' => 'colorswatch/product/list/options.phtml'
        ));
    
        $block->setProduct($_item);
    
        echo $block->toHtml();
    }?>
    

    Hope that will help you.