Hiding a Tab If It Is Left Empty

tabs

I have created new custom tabs on my product pages (Datasheets, Ask A Question and Returns and delivery). At the moment in my Datasheet tab it displays the PDF or datasheet for that product but if there is no datasheet in the tab is displays a message. Is there any way iu can get the tab to hide if there isnt a datasheet in the tab instead of diaplying a message??

This is how the tab is created :

<reference name="product.info.additional">
            <action method="unsetChild" ><name>product_tag_list</name></action>
            <block type="catalog/product_view_tabs" name="product.info.tabs" as="info_tabs">
                <action method="setTemplate" ><template>catalog/product/view/tabs.phtml</template></action>
        <action method="addTab" translate="title" module="catalog" ><alias>pdf_tabbed</alias><title>Data Sheets</title><block>catalog/product_view_attributes</block><template>catalog/product/view/pdf.phtml</template></action>
                </block>
</reference>

This is my phtml file. This is what is displayed in the tab.

<?php $_pdf = $this->getProduct()->getPdf(); ?>
<?php if ($_pdf): ?>
    <h2><?php echo $this->__('PDF') ?></h2>
    <div class="std">
        <?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_pdf, 'pdf') ?>
    </div>
<?php else: ?>
    Sorry but there's no Data Sheet associated with this product!
<?php endif; ?>

Thank you if you can help or point me in the right direction.

Best Answer

You could just add a tab conditionally trough an observer:

Observe the event core_block_abstract_prepare_layout_after

Check for the catalog tabs block instance and add your tab when the datasheet has data.

           public function core_block_abstract_prepare_layout_after(Varien_Event_Observer $observer) {
               $block = $observer->getEvent()->getBlock();

               if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs) {
                    //add if data not empty condition
                        $block->addTab('tabname', array(
                               'label' => Mage::helper('adminhtml')->__('tablabel'),
                               'title' => Mage::helper('adminhtml')->__('tabtitle'),
                               'content' => $block->getLayout()->createBlock('myblock', 'myblockname')->toHtml()
                           ));
               }
           }

I appologize, this was for the backend, not the frontend as the op asked.

Related Topic