Search code examples
phpxmlmagentomagento-1.5frontend

Conditionally Displaying Blocks In Product Page Tabs


I'm changing the front-end layout of a Magento 1.5 site, and I'm trying to move my products' reviews from being a separate page to being one of the 'more information' tabs on the lower part of the product page in the catalog. As a result, I want to make this tab's contents conditional - I want the tab to display existing reviews when they exist, but to display the review-creating form when the product has no reviews yet.

Here's what I've tried so far, the pieces I'm trying to put together.

/app/design/frontend/our-site/default/review.xml:

<catalog_product_view translate="label">
    <label>Catalog Product View</label>
    <reference name="product.info.tabs">
        <action method="addTab" translate="title" module="review">
            <alias>product.reviews</alias>
            <title>Reviews</title>
            <block>review/form</block>
            <template>review/form.phtml</template>
        </action>
    </reference>
</catalog_product_view>                                            

This XML results in the 'leave your review' form being displayed under a tab called "Reviews" on the product page.

I tracked down the 'addTab function in /app/code/core/Mage/Catalog/Block/Product/View/Tabs.php and I can kinda-sorta see how that works. However, I don't think I've seen a way to use the XML to be conditional about what should be displayed on a page.

I noticed that the summary.phtml file does something like what I want: it has:

if ($this->getReviewsCount()): ?>
    <div class="ratings">
        <?php if ($this->getRatingSummary()):?>
             <div class="rating-box">
                <div class="rating" style="width:<?php echo $this->getRatingSummary() ?>%"></div>
             </div>
    <?php endif;?>
         <p class="rating-links">
             <a href="<?php echo $this->getReviewsUrl() ?>"><?php echo $this->__('%d Review(s)', $this->getReviewsCount()) ?></a>
             <span class="separator">|</span>
             <a href="<?php echo $this->getReviewsUrl() ?>#review-form">
                 <?php echo $this->__('Add Your Review') ?></a>
         </p>
    </div>
<?php elseif ($this->getDisplayIfEmpty()): ?>
    <p class="no-rating">
        <a href="<?php echo $this->getReviewsUrl() ?>#review-form">
            <?php echo $this->__('Be the first to review this product') ?>
        </a>
    </p>
<?php endif; ?>

That looks like it has conditional functions for 'do this if there are reviews, do otherwise if there are no reviews. How can I invoke that earlier in the rendering chain to decide what the contexts of the block that gets rendered into the page will be? Is this a task that needs to be done in the phtml part of in the XML part?


Solution

  • You will probably need to create your own phtml file, copy that summary one as a starting point, but in the else, instead of rendering a link render the Block that is included on that other page. You can then adjust the xml file for that block to tell it you use your new template file by replacing the template attribute. You could add the review form block as a child using the xml layout and then use echo $this->getChildHtml('as_field_of_block');, or alternatively just hard code this into the template. The former option is probably more flexible, but will require a little extra tweaking of the xml.