2

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.

1

2 Answers 2

6

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.

2
  • Thanks @marius that is a big help. Your code doesn't work exactly as is but it did made me realise I need to look through my template's layout files in order to work out what reference names to use.
    – BlueC
    Jan 30, 2014 at 11:13
  • @BlueC I thought it might now work perfectly but at least you have the layout handles and block names that use price blocks.
    – Marius
    Jan 30, 2014 at 12:06
1

If you want to change the template on the product detail page, here is an example of a simple product:

<PRODUCT_TYPE_simple>
    <reference name="product.info.simple">
        <action method="addPriceBlockType">
            <type>simple</type>
            <block>catalog/product_price</block>
            <template>[whatever]/custom-price.phtml</template>
        </action>
    </reference>
</PRODUCT_TYPE_simple>

And if you don't know where to put the addPriceBlockType in, then you should have a look at the method _getPriceBlock and _getPriceBlockTemplate within app/code/core/Mage/Catalog/Block/Product/Abstract.php. You can check there to which layout name the block belongs and so you get the node name where to insert the price block type method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.