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();
    }
}

Answers


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.


Need Your Help

How to insert data from nested form to 2 tables rails 4

ruby-on-rails ruby-on-rails-4 devise nested-forms nested-attributes

I know this question has been asked number of times so far and I have tried figured out my solution from them but none of them worked for me.

Transaction between SQL Azure and Azure Table Storage i.e Rollback and Commmit

azure azure-sql-database azure-table-storage

At my current project we are working on a distributed cloud application that will store the state of an entity and the associated events. When a message is recieved from the queue we update the sta...