Magento – Add a tab on product page based on attribute set

attribute-setproducttabs

I would like to add a static block on the frontend product page conditionally, based on the attribute set of the product. (e.g. Helmets -> display helmet size chart, which does not apply to other products.)

I have added:

<reference name='product.info.tabs'>
    <block type="cms/block" name="helmets_size_chart">
        <action method="setBlockId"><block_id>helmets_size_chart</block_id></action>
    </block>
</reference>

to the tabs block in local.xml, but that shows the static block on the tabs section on all product pages.
Secondly, I've added:

protected function _prepareLayout() {
    $product = Mage::registry('current_product');

    if (!($setId = $product->getAttributeSetId())) {
        $setId = $this->getRequest()->getParam('set', null);
    }

    if($setId != 12) { // helmets
        $this->getLayout()->unsetBlock('helmets_size_chart');
    }
}

to app/code/local/Mage/Catalog/Block/Product/View/Tabs.php
When viewing the product page, I can verify indeed the code in the setId != 12 block is parsed, but the unsetBlock() call doesn't do anything.

As you may have guessed, this is my first attempt at something programmatically in Magento, and I have no clue if I'm on the right track. Any thoughts welcome!

Thanks

Best Answer

Create a custom block and extend from Mage_Cms_Block_Block

You can now do something like this:

class Foo_Bar_Block_Producttab extends Mage_Cms_Block_Block
{
    protected function _toHtml()
    {
        $product = Mage::registry('current_product');

        if ($product->getAttributeSetId() == 12) {
            $this->setBlockId('helmets_size_chart');
        }

        return parent::_toHtml();
    }
}

And adjust your layout update file:

<reference name='product.info.tabs'>
    <block type="foo_bar/producttab" name="helmets_size_chart" />
</reference>
Related Topic