0

I am creating a random list of products that refreshes every hour. I've got most of it done but there is one problem: getPriceHTML is returning NULL. This function is used in the normal category product lists and works perfectly there.

This code does give a correct getPriceHTML

$_productCollection = Mage::getModel('catalog/product')->getCollection();

But what I need is to get a random product, no matter what category it is in, so that is not an option.

The following code is the code I use to get random products:

$this->_productCollection = Mage::getResourceModel('catalog/product_collection');
Mage::getModel('catalog/layer')->prepareProductCollection($this->_productCollection);
$this->_productCollection->getSelect()->order('rand()');
$this->_productCollection->addStoreFilter();
$this->_productCollection->setPage(1, 3);

And later on:

foreach($this->_productCollection AS $_product)
{
    echo $this->getPriceHtml($_product, true);
}

For simple products this does give the correct price, but it returns NULL for grouped products. Yes, I am extending to Mage_Catalog_Block_Product.

Please keep in mind that I know that there are other functions to get the price, but I specifically want to know why this isn't working as it should.

  • The default method looks like this "public function getPriceHtml($product)" Do you have customised the getPriceHtml method that requires the additional true param? – Kristof at Fooman Jan 8 '14 at 8:41
  • I will check it out, If so, I have not done it myself. This is the code that has been used in the list.phtml – Deep Frozen Jan 8 '14 at 8:42
  • @Fooman I am not seeing why they added the true at that function, since it doesn't seem to be overridden in any way. Removing it still gives the same result too anyway. – Deep Frozen Jan 8 '14 at 8:47
1

Please take a look at the two implementations for getPriceHtml()

Mage_Catalog_Block_Product

public function getPriceHtml($product)
{
    $this->setTemplate('catalog/product/price.phtml');
    $this->setProduct($product);
    return $this->toHtml();
}

vs.

Mage_Catalog_Block_Product_Abstract

public function getPriceHtml($product, $displayMinimalPrice = false, $idSuffix = '')
{
    return $this->_preparePriceRenderer($product->getTypeId())
        ->setProduct($product)
        ->setDisplayMinimalPrice($displayMinimalPrice)
        ->setIdSuffix($idSuffix)
        ->toHtml();
}

extending from the Abstract block should help in getting the correct renderer.

  • As a sidenote Mage_Catalog_Block_Product does not extend from the Abstract block, sigh... – Kristof at Fooman Jan 8 '14 at 8:50
  • So that is where the true came from, that explains a lot! Extending to Mage_Catalog_Block_Product_Abstract did fix it for me, thanks! – Deep Frozen Jan 8 '14 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.