0
votes

I want to display specific HTML blocks on all product pages of a specific category in Magento.

For example:

Categories - dress, phone, gps HTML blocks for these categories: dress.phtml, phone.phtml, gps.phtml

On all the pages of products in dress, dress.phtml is displayed - dress size charts, color charts, how to wash, etc.

On all the pages of products in phone, phone.phtml is displayed - battery care, how we package the phones, etc.

On all the pages of products in gps, gps.phtml is displayed - GPS usage, installation, etc.

You get the idea. How can I do this in Magento? How can I call different PHTML files (dress.phtml, phone.phtml or gps.phtml) in catalog/product/view.phtml by product category?

Thanks!

2

2 Answers

1
votes

This is easily achieved by making a few modificaitions to your catalog/product/view.phtml file and your custom theme's layout/local.xml file.

Let's begin by modifying local.xml. Open it up and type / copy & paste the following code:

<catalog_product_view>
    <reference name="content">
        <reference name="product.info">
            <block type="catalog/product_view" name="product.dress" as="productDress" template="wherever/you/keep/dress.phtml" />
            <block type="catalog/product_view" name="product.phone" as="productPhone" template="wherever/you/keep/phone.phtml" />
            <block type="catalog/product_view" name="product.gps" as="productGps" template="wherever/you/keep/gps.phtml" />
        </reference>
    </reference>
</catalog_product_view>

What we're essentially doing by modifying local.xml is making your new templates available to the catalog/product/view.phtml file within the product.info block. The block type is *catalog/product_view* so we have access to all the same information and methods available within catalog/product/view.phtml. We can then write a bit of code in view.phtml to decide what block to output

$_currentCategory = Mage::registry('current_category');
if ($_currentCategory->getId() == id of dress category) {
    echo $this->getChildHtml('productDress');
} elseif ($_currentCategory->getId() == id of phone category) {
    echo $this->getChildHtml('productPhone');
} elseif ($_currentCategory->getId() == id of gps category) {
    echo $this->getChildHtml('productGps');
}

All we're doing here is checking the current category of the product and outputting the associated block. Of course you'll need to replace "id of ___ category" with the appropriate category ID value. This won't work if you're in a child category of one of the categories you'd like to find. For example, if your structure is as follows Phone > Accessories > Phone Charger Product we'll only find the latest category (Accessories) in the Phone Charger Product. There's other ways of finding a parent category which are scattered across Google and StackOverflow.

Hope that helps. I don't have means to test this code unfortunately but it should work. Good luck!

0
votes

$product->getCategoryIds() can give you the category ids to which the product belong.