2

I have followed this answer to hide products without images on the category listing page. It worked nicely for a while.

Now, for some reason it seems the products without images are still showing up on the listing page. Any ideas as to why this may be happening?

Note: The same list.phtml page is being used.

Thank you.

Community
  • 1
  • 1
shnozolla
  • 468
  • 10
  • 20

3 Answers3

4

Add the following to list.phtm:

//$_productCollection=$this->getLoadedProductCollection();
$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
                   ->addAttributeToFilter('small_image', array('neq' => 'no_selection'))
                   ->load();

This answer recommended the following:

->addAttributeToFilter('image', array('neq' => 'no_selection'))

Whereas I have set it to:

->addAttributeToFilter('small_image', array('neq' => 'no_selection'))

The reason the previous answer did not work was because the product collection doesn't load the regular image, and therefore the regular image cannot be added as an attribute to filter, so instead, I added the small_image as the attribute to filter.

You can also try R.S's answer where he adds the image to the page and hence the collection. You may have to also add all attributes using:

->addAttributeToSelect('*')
Community
  • 1
  • 1
shnozolla
  • 468
  • 10
  • 20
3

There are some tricks to keeping Magento in line. One thing I've learned is that the Magento Model will change for many different reasons, and its kinda hard to figure out why. There are better ways to do this (modifying the collection, etc) but it sometimes just does not work and you don't have days to figure out why.

If you want a surefire way to make sure your image exists, use the following code... It may not be the 'magento way' but it works, and I tested it on my site (Magento EE 1.12). Put it in a function, or use it directly in your phtml if you want!

It basically just makes sure the URL exists.

$exists = false;
$entity_id = 8800;
$product = Mage::getModel('catalog/product')->load($entity_id);
$mediaUrl= Mage::getBaseUrl('media');
$imageUrl = $mediaUrl . "catalog/product" . $product->getImage();

$file_headers = @get_headers($imageUrl);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}

var_dump($exists);
var_dump($imageUrl);
echo '<img src="' . $imageUrl . '" />';

$exists will either be true (image does exist) or false (image does not)

CarComp
  • 1,929
  • 1
  • 21
  • 47
  • I was thinking of doing something along those lines, I tried your code, and for some reason $exists returns true every time...the products that don't have images, do have placeholders could this be the reason? – shnozolla Jan 09 '13 at 19:47
  • I don't think so, if you are referring to the normal "M" magento placeholders. If there is ANY image in there, even if it doesn't have the radio button checked in admin for base, small or thumbnail, it will exist. If all your product image says is "No Image" in admin, there should be no valid image. Please put a var_dump($imageUrl) and show me what it sends out. – CarComp Jan 09 '13 at 20:43
  • I edited the code above to not only display if it true / false, but also the url and the actual image that is supposedly there. – CarComp Jan 09 '13 at 20:55
  • var_dump($imageUrl) is giving me NULL (even for products with an image!?) – shnozolla Jan 09 '13 at 21:09
  • confirm in the code above that the $entity_id is an actual existing product of yours that you know has no image. var_dump($product->debug()); make sure you get some data. var_dump($mediaUrl) too. make sure all these have data. Also, make sure you are loading the product like i've shown in the code, even if its loaded. Don't rely on the load in the product collection to behave the exact same way (as the other answer suggests, you may be encountering this). – CarComp Jan 09 '13 at 21:11
  • Ok, I will try the above soon and i'll comment here with the results, strangely enough as I am working on other aspects of the site, I noticed that the products without images show, then moments later they do not show (I have done re indexing and cache refresh) but I cannot find the logical pattern, when they show or not. Sometimes when I re index, it has not effect on the imageless products. Thanks for your help. – shnozolla Jan 09 '13 at 21:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22483/discussion-between-carcomp-and-shnozolla) – CarComp Jan 09 '13 at 21:37
2

I think that the issue is that you are trying to get the 'image' (base image) property on the list.phtml (by default i can only access the thumbnail, small_image).

On list.phtml (not loading the full product resource like on view.pthml)

echo $_product->getImage() //null
echo $_product->getThumbnail() // path/name.jpg
echo $_product->getSmallImage() // path/name.jpg

I think you may need to add adding something like this to app/design/frontend/default/yourtheme/layout/catalog.xml

  <action method="addAttribute"><name>image</name></action>

See How to get access to custom Image Attribute in list.phtml in Magento

Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
  • I was trying to figure out how adding the main image to the list would solve this, after realizing where you were going with that, I simply changed the `->addAttributeToFilter('image', array('neq' => 'no_selection'))` to `->addAttributeToFilter('small_image', array('neq' => 'no_selection'))` and that seemed to make it work. Thanks. – shnozolla Jan 09 '13 at 21:26