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
Product collections by default load a subset of the products' attributes, unlike
$product->load()
which loads all product attributes. You have to make sure that themsrp
attribute is loaded by the product collection you're using. SeeMage/Catalog/etc/config.xml
, theconfig/frontend/product/collection/attributes
node:In your module's
config.xml
you can addmsrp
:Alternatively, you can tap into the block that loads the product collection and do a
$productCollection->addAttributeToSelect('msrp');
. Another option is to also add an observer to the product collection load before event and again add the attribute to the select./Edit: If System -> Configuration -> Catalog -> Frontend -> Use Flat Catalog Product is enabled, product collections on the frontend read from the flat tables. That means that the attribute has to be one of the attributes indexed this way otherwise it will be silently ignored (addAttributeToSelect does nothing). An attribute to be indexed in the flat tables has to meet a few conditions that I don't remember exactly right now. I think the attribute has to be
Used in product listing
,Use in Layered Navigation
and something else.Cheers
/Edit: adding information from the comments
getAttributeText()
should be used only for Dropdown and Multiple Select attributes. I'm pretty sure MSRP is a price type attribute.getAttributeText()
eventually callsgetOptionText()
which returnsfalse
for regular attributes.See
Mage_Catalog_Model_Product::getAttributeText()
andMage_Eav_Model_Entity_Attribute_Source_Abstract::getOptionText()