Magento _use getPriceHtml() function in custom page template outside the list or product view page
16512
single,single-post,postid-16512,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-5.9,wpb-js-composer js-comp-ver-4.3.4,vc_responsive

Magento _use getPriceHtml() function in custom page template outside list or product view page

custom_configurable_product_magento

03 Aug Magento _use getPriceHtml() function in custom page template outside list or product view page

Magento _use getPriceHtml() function in custom page template outside list or product view page 

Magento uses blocks terminology in its architecture to display the views. Blocks are classes in Magento modules which handles the content output and communicate directly with phtml templates. Data and information is passed to the template through Block methods, implemented in the block class.

$this->getPriceHtml() method is used in the templates to display the product price in html format. This method returns the rendered html with currency symbol and defined number format. It also display the products special price and minimum price. However this is a very useful method for custom created templates or content blocks where we need to display the formatted price but still this function cannot be used directly with the $product object.

By default this function is available on List and Product View pages where it can be used as $this->getPriceHtml($_product);.

To use this function on any custom template page or even in a controller, use the following code.

 

Update

After version 1.8.0.0, Magento updated both core and theme files. The above code uses the theme file app/design/frontend/yourpackage/yourtheme/template/catalog/product/price.phtml  which now has a new code near line number 53

$_specialPriceStoreLabel = $this->getProductAttribute(‘special_price’)->getStoreLabel();.

Using the above code will throw the fatal error:- Fatal error: Call to a member function getStoreLabel() on a non-object in \app\design\frontend\package\theme\template\catalog\product\price.phtml on line 53

Solution

To solve this problem we need to modify the price.phtml file. Open the file app/design/frontend/package/theme/template/catalog/product/price.phtml.

 

How to use

Copy this code where you want to display the price in html format.

How it works

new Mage_Catalog_Block_Product; instantiate the Product block class which contains the method getPriceHtml();. As argument this method takes the product object.

 hope this article will help you, thanks!

Jenny

Jenny Tran
hatran260612@gmail.com
No Comments

Post A Comment