Magento – Add Custom Product Tab in MAGENTO 1.9 CE

local.xmlmagento-1.9producttabs

Version – 1.9

Theme : rwd

I followed this to add custom product tabs in product detail page. To add a new block I added below code in local.xml, but somehow it only works if I add it in catalog.xml

<catalog_product_view>
        <reference name="product.info">
            <block type="catalog/product_view_attributes" name="features_product_tab" as="features_tab" template="catalog/product/view/features.phtml">
                <action method="addToParentGroup"><group>detailed_info</group></action>
                <action method="setTitle" translate="value"><value>Features</value></action>
            </block>
        </reference>
        </catalog_product_view>

Adding the <block> in catalog.xml only, makes it appear as new tab.

In order to add content by admin, I have created an atrribute, assigned it to General group.

Created a CMS block at CMS > Static Blocks > Add New Block and in the create/edit product view, locating new attribute and populate it with the identifier of new CMS block. In my case, features_product_tab.

When I disable the Static Block, the tab still appears on product page. And even if the content is null, its shows on page. Maybe (as is often the case) CMS block is not strictly empty and contains hidden div or p tags?

The code in local.xml to display one of the tabs is

<block type="catalog/product_view_attributes" name="features_product_tab" as="features_product_tab" template="catalog/product/view/tabs/features.phtml">
                <action method="addToParentGroup"><group>detailed_info</group></action>
                <action method="setTitle" translate="value"><value>Features</value></action>
            </block>
  1. Is it possible to check the Block Status from xml code, and show
    it only if its enabled ?
  2. How can the value of 'setTitle' be fetched from Block Title ?

Best Answer

Please only ask one question at once. Anyway, here you go:

  1. The following content for the local.xml works. It will add a new tab on the product page. If it does not work, your layout XML file is probably not loaded correctly.

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <catalog_product_view>
            <reference name="product.info">
                <block type="catalog/product_view_attributes" name="features_product_tab" as="features_tab" template="catalog/product/view/features.phtml">
                    <action method="addToParentGroup"><group>detailed_info</group></action>
                    <action method="setTitle" translate="value"><value>Features</value></action>
                </block>
            </reference>
        </catalog_product_view>
    </layout>
    
  2. This is not possible in a nice way. But you could write something unique into the static block to mark it empty and then change the way Magento renders the tabs in order to hide "empty" tabs. Therefore, write e.g. EMPTY into the static block you do not want to be rendered. Magento will automatically add a paragraph tag around it, so it becomes <p>EMPTY</p>. Then, in your catalog/product/view.phtml change the tab rendering like that:

    <div class="product-collateral toggle-content tabs">
        <?php if ($detailedInfoGroup = $this->getChildGroup('detailed_info', 'getChildHtml')):?>
            <dl id="collateral-tabs" class="collateral-tabs">
                <?php foreach ($detailedInfoGroup as $alias => $html):?>
                    <?php if ($html != '<p>EMPTY</p>'): ?>
                        <dt class="tab"><span><?php echo $this->escapeHtml($this->getChildData($alias, 'title')) ?></span></dt>
                        <dd class="tab-container">
                            <div class="tab-content"><?php echo $html ?></div>
                        </dd>
                    <?php endif; ?>
                <?php endforeach;?>
            </dl>
        <?php endif; ?>
    </div>