Magento CE 1.7.0.2 – Override price.phtml Template Using addPriceBlockType

ce-1.7.0.2layoutpricetemplatexml

I'm writing a custom extension and trying to override the price.phtml template (catalog/product/price.phtml) with my own but I cannot get it to work.

The documentation and examples I have seen suggest I should use the addPriceBlockType method in the layout update xml file as shown below. However, this doesn't work and it is driving me mad.

Other layout updates work so I know the layout xml file is being called correctly from the module's config.xml.

One thing – I think in older versions of Magento there was different product price template files for each of the product types but this is no longer the case in 1.7? Could that mean that addPriceBlockType will no longer work and I need to use some other method?

Any help would be much appreciated.

Thanks!
Chris

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="catalog_product_price_template">
            <action method="addPriceBlockType"><type>simple</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
            <action method="addPriceBlockType"><type>grouped</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
            <action method="addPriceBlockType"><type>configurable</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
            <action method="addPriceBlockType"><type>virtual</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
            <action method="addPriceBlockType"><type>bundled</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
        </reference>
    </default>

...

Update: Thanks to Marius below for shedding some light on this. I have now found another approach which to me is much cleaner than messing round with the insanely complicated xml layouts.

In my module I have extended Mage_Catalog_Block_Product_Price and added a call to

$this->setTemplate('hideprice/price.phtml')

in the _toHtml() function. This single change ensures that my new price.phtml template is used everywhere.

Best Answer

Your reference is name is wrong.
Also the reference name depends on the page you are viewing.
Here is how your layout file should look.

<?xml version="1.0"?>
<layout>
    <catalog_category_default>
        <reference name="product_list">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </catalog_category_default>

    <catalog_category_view>
        <reference name="product_list">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </catalog_category_view>

    <catalog_category_layered>
        <reference name="product_list">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </catalog_category_layered>

    <catalog_product_compare_index>
        <reference name="catalog.compare.list">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </catalog_product_compare_index>

    <catalogsearch_result_index>
        <reference name="search_result_list">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </catalogsearch_result_index>

    <catalogsearch_advanced_result>
        <reference name="search_result_list">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </catalogsearch_advanced_result>

    <tag_product_list>
        <reference name="search_result_list">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </tag_product_list>

    <tag_customer_view>
        <reference name="customer_view">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </tag_customer_view>

    <default>
        <reference name="cart_sidebar">
            <!-- your addPriceBlockType methods here-->
        </reference>
        <reference name="wishlist_sidebar">
            <!-- your addPriceBlockType methods here-->
        </reference>
        <reference name="catalog_product_price_template">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </default>

    <catalog_product_view>
        <reference name="catalog.product.related">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </catalog_product_view>
    <PRODUCT_TYPE_simple>
        <reference name="product.info.upsell">
             <!-- your addPriceBlockType methods here-->
        </reference>
    </PRODUCT_TYPE_simple>
    <checkout_cart_index>
        <reference name="checkout.cart.crosssell">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </checkout_cart_index>
    <PRODUCT_TYPE_bundle translate="label" module="bundle">
        <reference name="product.info.bundle"> 
            <!-- your addPriceBlockType only for bundle here-->
        </reference>
        <reference name="product.clone_prices">
            <!-- your addPriceBlockType only for bundle here-->
        </reference>
    </PRODUCT_TYPE_bundle>
    <rss_catalog_category>
        <reference name="rss.catalog.category">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </rss_catalog_category>
    <rss_catalog_new>
        <reference name="rss.catalog.new">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </rss_catalog_new>
    <rss_catalog_tag>
        <reference name="rss.catalog.tag">
            <!-- your addPriceBlockType methods here-->
        </reference>
    </rss_catalog_tag>
</layout>

I hope I've covered all the cases. Good luck.