使用if语句在local.xml中显示/隐藏产品选项卡


Show / Hide product tab using if statement in local.xml Magento 1.9 CE

我对magento相当陌生,使用CE 1.9。

我知道如何通过local.xml在产品页面上删除/添加选项卡然而,我正试图隐藏/显示基于产品属性值的选项卡。

我已经成功创建了一个自定义选项卡。另外,customTab。我创建了一个php文件,我能够得到一个if语句成功地工作…种…

下面是我在php文件中的代码:
    <?php
    $staticBlockId = 'block_product_tab2';
    $product = Mage::registry('current_product'); ?>
<?php   if($product->getRepairservice()): ?>
            <div class="std"><?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($staticBlockId)->toHtml(); ?></div>
<?php   endif; ?>

这将根据属性的布尔值成功地隐藏或显示选项卡的"内容"。然而,它仍然显示选项卡,它只是空的。

所以我想我需要去它在local.xml中创建的地方,但我不确定如何构建if语句或访问属性。

我认为最清楚的方法是通过创建自己的布局句柄,参见:http://inchoo.net/magento/custom-layout-update-handles/。你必须检查你是否在产品页面上然后检查你的getRepairservice(),例如:

// Inside the controllerActionLayoutLoadBefore() function of Inchoo's example
$layout = $observer->getEvent()->getLayout();
$product = Mage::registry('current_product');
if($product && $product->getRepairservice())
{
    $layout->getUpdate()->addHandle('REPAIR_SERVICE')
}

之后,您可以使用XML很好地添加选项卡。

<REPAIR_SERVICE>
    <reference name="product.info">
        <block type="catalog/product_view_description" name="product.new.tab" as="new.tab" template="catalog/product/view/mynewcustomtab.phtml">
            <action method="addToParentGroup"><group>detailed_info</group></action>
            <action method="setTitle" translate="value"><value>Custom Tab</value></action>
        </block>
    </reference>
</REPAIR_SERVICE>