Random product collection, getPriceHTML gives wrong values
-
16-10-2019 - |
Question
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.
Solution
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.