Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - use an alternate "price.phtml" (in addition to the existing one)

Tags:

magento

I am looking for a way to have an alternate template/catalog/product/price.phml used in one specific location, and to continue using the existing price.phtml file in all other locations.

To explain further, I need to display the regular price, and then another special price right below it - but only on the product page (for the main product being displayed). This special price is not a price that can be calculated by the catalog price rules, so I wrote my own module to do the calculation. So, everywhere that I am displaying prices I want to display with the regular ol' template/catalog/product/price.phtml file... but for the product page (the main product - not the related, upsells, etc) I want to use my own custom template/catalog/product/price-custom.phtml template file. Can anybody help?

Normally I just look in the layout xml files (for example catalog.xml) to find these types of things, but price.phtml is kinda special - it isn't that simple. And for the life of me I can't figure out if there is an easy way to swap it out conditionally on the page being viewed. I am aware that I can just update price.phtml to always print out this extra price, and then use css to hide the price everywhere, but I would rather not do that if possible.

(Also you may want to know that I only have simple products.)

like image 464
shaune Avatar asked Jan 20 '11 14:01

shaune


4 Answers

This can be done in a layout XML file:

<layout>
    <PRODUCT_TYPE_simple>
        <reference name="product.clone_prices">
            <action method="setTemplate">
                <template>catalog/product/price-custom.phtml</template>
            </action>
        </reference>
    </PRODUCT_TYPE_simple>
</layout>
like image 123
clockworkgeek Avatar answered Nov 12 '22 10:11

clockworkgeek


Create a local.xml file, put it in app/frontend/default/YOURTEMPLATE/layout

In the local.xml file, add:

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <!-- Override price template on product view page -->               
    <PRODUCT_TYPE_simple>
        <reference name="product.info.simple">
            <action method="setTemplate">
                <template>catalog/product/price_product_page.phtml</template>
            </action>
        </reference>
    </PRODUCT_TYPE_simple>
    <!-- /Override price template on product view page -->              
</layout>

Create a copy of catalog/product/price.phtml and put it in YOURTEMPLATE/templates/product/product_price_page.phtml

This will override the price.phtml in the template, and replace it with product_price_page.phtml

like image 41
Ryan Lucier Avatar answered Nov 12 '22 09:11

Ryan Lucier


Or in your php block.

See example here :

Mage_Catalog_Block_Product_Abstract

protected $_priceBlockDefaultTemplate = 'catalog/product/price.phtml';
protected $_tierPriceDefaultTemplate  = 'catalog/product/view/tierprices.phtml';
like image 1
Chaabelasri E. Avatar answered Nov 12 '22 11:11

Chaabelasri E.


I had a similar requirement recently, where a different price template for the product page was the preferred solution.

The price block appears to be something of a special case in Magento (in the RWD theme at least), it's defined in catalog.xml as simply a block type and name within the <default/> handle:

<block type="catalog/product_price_template" name="catalog_product_price_template" />

If you look around at how some core layout files set the price template, you'll find examples like this (from bundle.xml):

<reference name="catalog_product_price_template">
    <action method="addPriceBlockType">
        <type>bundle</type>
        <block>bundle/catalog_product_price</block>
        <template>bundle/catalog/product/price.phtml</template>
    </action>
</reference>

They call a method called addPriceBlockType which you can find in Mage_Catalog_Block_Product_Abstract

Based on this and after a little experimentation, I found the following solution worked for me:

<catalog_product_view>
    <reference name="product.info">
        <action method="addPriceBlockType">
            <type>simple</type>
            <block>catalog/product_price</block>
            <template>catalog/product/price_product_page.phtml</template>
        </action>
        <action method="addPriceBlockType">
            <type>configurable</type>
            <block>catalog/product_price</block>
            <template>catalog/product/price_product_page.phtml</template>
        </action>
        <!-- Set for each product type as necessary e.g. bundled, virtual etc... -->
    </reference>
</catalog_product_view>
like image 1
fracture Avatar answered Nov 12 '22 09:11

fracture