Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - using $this->getPriceHtml on custom page template

Tags:

php

magento

I have a scroller showing a collection of products currently on sale, which I call using the following:

$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', $visibility)
    ->setPageSize(4) // Only return 4 products
    ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
    ->addAttributeToFilter('special_to_date', array('or'=> array(
           0 => array('date' => true, 'from' => $todayDate),
           1 => array('is' => new Zend_Db_Expr('null')))
           ), 'left')
    ->addAttributeToSort('special_from_date', 'desc');
$_productCollection->load();

I then run a foreach to get the individual products:

foreach ($_productCollection as $_product)

Everything works fine, except for the price, which I would usually call using

$this->getPriceHtml($_product, true)

However this is giving me a blank. If I do a var_dump I can see that both the original price and the special price are both available, so why isn't this working? I use exactly the same code on my homepage template, which I call through the homepage CMS, and the price is shown fine (with the regular price crossed out and special price shown).

Using $_product->getFinalPrice() works fine, but only gives me the final "special" price and doesn't show the original price.

Am I maybe missing something in my xml layout that's needed to show the prices using getPriceHtml?

like image 994
Marlon Creative Avatar asked Jan 10 '11 23:01

Marlon Creative


1 Answers

My colleague recommended using this Magento friendly method to get the price html anywhere:

<?php $_product = Mage::getModel('catalog/product')->load($product->getId());
      $productBlock = $this->getLayout()->createBlock('catalog/product_price');
      echo $productBlock->getPriceHtml($_product); ?>

If you're already working with a loaded product then you won't need the first line, however my product was from a collection so this was necessary.

like image 176
John Tilley Avatar answered Sep 17 '22 18:09

John Tilley