Magento – Hide empty custom tabs

magento-1.9tabs

I have added size chart tabs in magento 1.9.1 using the code below to call a CMS block into the product detail, but would like to hide the tab, if there is no data.

I am stuck and can't seem to find a method to hide the tab, if the attribute is empty.

I'm not to good with conditionals, I've tried (!empty) but maybe I'm trying the wrong place.

Any assistance is greatly appreciated.

Here's the code I am using:

catalog.xml

<block type="catalog/product_view_attributes" name="product.sizes" as="sizes" template="catalog/product/view/sizes.phtml">
    <action method="addToParentGroup"><group>detailed_info</group></action>
    <action method="setTitle" translate="value"><value>Size Guide</value></action>
</block>

And here's the size.phtml

    <?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('sizes_table');
if ( is_object($attribute) ) {
  $identifier = $_product->getData("sizes_table");
}
?>

<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
    <div class="std">
        <?php echo $_sizeBlock->toHtml() ?>
    </div>
<?php endif; ?>

Best Answer

Try this, it's a bit funky but it works. The idea is to add new tab only if sizes_table is set on the current product instance.

In your layout update file (local.xml):

<catalog_product_view>
    <reference name="product.info">
        <block type="foo_bar/block" name="product.sizes" as="product.sizes" />
    </reference>
</catalog_product_view>

Custom block class (app/code/local/Foo/Bar/Block/Block.php):

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

        if ($product->getData('sizes_table')) {
            $this->setBlockAlias('product.sizes');
            $this->setParentBlock($this->getLayout()->getBlock('product.info'));
            $this->addToParentGroup('detailed_info');

            /** tab title */
            $this->setTitle($this->__('Size Guide'));

            /** static block identifier */
            $this->setBlockId('my_static_block');
        }

        return $this;
    }
}

If the Size Guide tab is not added try commenting out if ($product->getData('sizes_table')) condition. If it's added after that, you'll have to find another way to retrieve the product attribute.

Related Topic