I’m working with Magento 1.7.0.2 and I’m trying to display a custom image from my products in a random product_list.
First of all I added the attribute to all my sets and set a placeholder image.
In the database I searched for *used_in_product_listing* and set it to 1.
This is my Product_List_Random Class:
class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$categoryID = $this->getCategoryId();
if($categoryID) {
$category = new Mage_Catalog_Model_Category();
$category->load($categoryID);
$collection = $category->getProductCollection();
} else {
$collection = Mage::getResourceModel('catalog/product_collection');
}
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->addAttributeToSelect('*');
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
$collection->setPage(1, $numProducts)->load();
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
}
Here I tried to add $collection->addAttributeToSelect('*'); to get the url of the image in frontend.
This is what I tried so far in frontend:
$_productCollection=$this->getLoadedProductCollection();
foreach ($_productCollection as $_product):
echo $this->helper('catalog/image')->init($_product, 'specialprice_image')->resize(158, 270);
echo $_product->getResource()->getAttribute('specialprice_image')->getFrontend()->getValue($_product);
echo $_product->getAttributeText('specialprice_image');
$product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
echo $product->getAttributeText('specialprice_image');
endforeach;
The helper is giving me the placeholder url. Ther other methods are giving me nothing.
I hope someone can help me with this problem.
Now it works after editing the Mage_Catalog_Block_Product_List_Random Class
I just removed two lines of code:
The complete code:
And in frontend I’m using this code: