I am looping through products and getting an error with product images.
The code I am working with is below:
<?php
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('status', 1); // 1=Enabled, 2=Disabled
$obj = Mage::getModel('catalog/product');
?>
<?php $productCounter=1; ?>
<?php foreach ($collection as $product): ?>
<?php $_productInCollection = $obj->load($product->entity_id); ?>
<li class="item text-center">
<a class="product-image" title="<?php echo $_productInCollection->getName(); ?>" href="<?php echo $_productInCollection->getProductUrl(); ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_productInCollection, 'small_image')->resize(null, 152)->keepFrame(false); ?>" height="152" alt="<?php echo $this->stripTags($this->getImageLabel($_productInCollection, 'small_image'), null, true) ?>" />
</a>
</li>
<?php if ($productCounter >=26) : ?>
<?php break; ?>
<?php endif; ?>
<?php $productCounter ++; ?>
<?php endforeach; ?>
Attributes such as name are pulling through fine but I'm getting a strange error with product images. About 75% of the products I am working with do not have images so should show the default placeholder image, but I'm getting a strange pattern:
PRODUCT 1 -> has no image in admin panel -> SHOWS PLACEHOLDER (correct)
PRODUCT 2 -> has no image in ap -> SHOWS PLACEHOLDER (correct)
PRODUCT 3 -> has an image in ap -> SHOWS PRODUCT 3 IMAGE (correct)
PRODUCT 4 -> has no image in ap -> SHOWS PRODUCT 3 IMAGE (wrong, should show placeholder)
PRODUCT 5 -> has no image in ap -> SHOWS PRODUCT 3 IMAGE (wrong, should show placeholder)
PRODUCT 6 -> has no image in ap -> SHOWS PRODUCT 3 IMAGE (wrong, should show placeholder)
PRODUCT 7 -> has an image in ap -> SHOWS PRODUCT 7 IMAGE (correct)
PRODUCT 8 -> has no image in ap -> SHOWS PRODUCT 7 IMAGE (wrong, should show placeholder)
PRODUCT 9 -> has no image in ap -> SHOWS PRODUCT 7 IMAGE (wrong, should show placeholder)
PRODUCT 10 -> has an image in ap -> SHOWS PRODUCT 10 IMAGE (correct)
As soon as a product which has an image appears in the loop, that product appears fine, but if the next product in the loop has no image, it shows the previous product image rather than the placeholder. I have tried it with getImageUrl()
too and the same pattern occurs.
I wonder if it is a caching problem or just that the image is not getting reset properly each time? Any help would be greatly appreciated, thank you!
I am using Magento ver. 1.7.0.2.
EDITED - SOLVED: Thanks to Marius below, this is the code I am using.
<?php
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('status', 1);
// 1=Enabled, 2=Disabled
$collection->getSelect()->limit(25);
$productBlock=$this->getLayout()->createBlock("catalog/product");
?>
<?php $productCounter = 1; ?>
<?php foreach ($collection as $product): ?>
<?php if (($productCounter + 4) % 5 == 0) : ?>
<ul class="products-grid">
<?php endif; ?>
<li class="item text-center">
<h3><?php echo $product->getName(); ?></h3>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(null, 152)->keepFrame(false); ?>" height="152" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />
<?php echo $productBlock->getPriceHtml($product,true); ?>
<a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $product->getName(); ?>" class="more-info">
More info
</a>
</li>
<?php if ($productCounter % 5 == 0 || $productCounter == $collection.length) : ?>
</ul>
<?php endif; ?>
<?php endforeach; ?>
load()
ing in a loop! Get the collection to load the attribute information which you need.