Question

Catalog Product listing page's template file is list.phtml. Where the rendering of all the products in category happens using foreach loop.

I am confused about the rendering of the price.phtml. Because there is no block for it in handle <catalog_category_default>

Now, <?php echo $this->getPriceHtml($_product, true) ?> returns price of the product.

How this method is linked to price.phtml ?

Was it helpful?

Solution

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() ?>">
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top