Product price display
The method getPriceHtml()
is defined in the block class Mage_Catalog_Block_Product_Abstract
.
The method uses a price renderer which depends on the product type:
$type_id = $product->getTypeId(); // e.g. simple, configurable, bundle, grouped, ...
$this->_preparePriceRenderer($type_id)
The price renderer is a block with an associated template. Both depend on the product type.
If there is no block or template defined for the specified product type, it defaults to the catalog/product_price
block and the catalog/product/price.phtml
template.
Where are the price renderers and templates for product types defined?
The price blocks and renderers usually are specified using layout XML, so modules which add new product types can easily add their own renderers, too. For example. the bundled product adds it's price renderer as follows in the bundle.xml
layout file:
<default>
<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>
</default>
Custom price templates or renderers
Using the same mechanism it's also possible to override the default renderers. If you want to just change the template, and keep the default price block type, simply call addPriceBlockType
in the layout file with the default block (see above) and your template.
Rendering the price block
The code to actually render the price block looks like this:
$this->_preparePriceRenderer($type_id)
->setProduct($product)
->setDisplayMinimalPrice($displayMinimalPrice)
->setIdSuffix($idSuffix)
->toHtml();
The $displayMinimalPrice
and $idSuffix
variables are optional arguments to the getPriceHtml
method.
public function getPriceHtml($product, $displayMinimalPrice = false, $idSuffix = '')
The $idSuffix
is used in the price template to complete the CSS id of the <span>
tag surrounding the actual price, for example:
<span class="price" id="price-excluding-tax-<?php echo $_id ?><?php echo $this->getIdSuffix() ?>">
Best Answer
This is my wild guess. Not sure whether it works or not. However it feels logical to me. First of all you are using
addPriceBlockType()
function, which is defined inMage_Catalog_Block_Product_Abstract
block class. Let us look what this method does.This method simply set
_priceBlockTypes
property with the given details. In our case, this property holds a values like this.Layout job is over. Value is set correctly in
Mage_Catalog_Block_Product_Abstract::$_priceBlockTypes
. Now we need to see how price template get rendering in frontend. For this take a look onlist.phtml
. You can see a code like this.In
related.phtml
, this block is invoked like this.Now it is important to know what
getPriceHtml()
does. Again this is a property ofMage_Catalog_Block_Product_Abstract
block class. Let us have a look.A close look reveals that, the method first retrieves product's type id and then use that value to prepare price section. ie
Now let us have a look what this protected
_preparePriceRenderer()
method does.This method gets the block according to the type id that passed and set a template to that block and returns it. If we didn't set a custom block for price block,
$this->_getPriceBlock($productType)
will use default blockMage_Catalog_Block_Product_Price
. In our case, this default block is using. Now this block is setting with a template using the method_getPriceBlockTemplate()
. Now let us have a look on that method.As you can see it returns a template, if a template is set based on the type. Else it uses default template
catalog/product/price.phtml
.This is the theory. Now let us think what is the problem exists here. We have template only for
simple
product type. Related products may be of any type. It may be simple, bundle, configurable etc. In the current situation, it uses your custom template only for simple related products. Not for any other types. So you need to set your custom template for every other product types[edit]
File :
app/design/frontend/<package>/<theme>/layout/local.xml
Hope that make sense !
edit 2
It is wierd that, this method doesn't work. However there is an alternative solution for this. If you are no longer going to use the default price template, you can rewrite
Mage_Catalog_Block_Product_Abstract
block class and change the default template. However there is another problem. We cannot rewrite this class because it is an abstract class. So the easiest way to overwrite the method is "recreate" the same class in local pool.File :
app\code\local\Mage\Catalog\Block\Product\Abstract.php
I have provided two ways to put your own custom price template. Use the last line, if the first condition fails again. If you are using the code, then you dont need to do any layout updates in anywhere. It will automatically use your custom price template every where it is displaying