1

I am looking to filter a product collection by the number of product images uploaded for each particular product. For example I want to get a collection of all products that have at least 2 product images uploaded.

Is this possible?

2
  • Try this $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('small_image',array('gt'=>'2')) //or ->addAttributeToSelect('thumbnail',array('gt'=>'2')) //or ->addAttributeToSelect('image',array('gt'=>'2'));
    – Magento 2
    Commented Feb 15, 2016 at 18:38
  • 1
    Hi Kothari, Thanks for looking at this. I'm not sure you're example will work because the image, small_image and thumbnail attributes will only ever return the URL of the image that is selected for that attribute. Commented Feb 16, 2016 at 11:09

2 Answers 2

0

You need to join the media gallery table and count matches. It does not look like there is an existing method for this, so you have to do the join like this:

$mediaResource = Mage::getResourceSingleton('catalog/product_attribute_backend_media');
$galleryTable = $mediaResource->getMainTable();

/* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->joinTable(
    ['gallery_table' => $galleryTable], 'entity_id=entity_id', ['gallery_id' => 'value_id'],
    null, 'left');

$collection->getSelect()->group('e.entity_id');
$collection->getSelect()->having('COUNT(*) >= 2');

If you want to know the exact number of images per product, you can also add this:

$collection->addExpressionAttributeToSelect(
    'image_count', 'COUNT(gallery_table.value_id)', []);
3
  • Hi Fschmengler, thanks for your response, This has put me on the right track. If I wanted to get a collection of products that do not have any images would you simply change the count to equal 0? Commented Feb 20, 2016 at 13:55
  • Not quite, because there is always at least one row in the group. But gallery_table.value_id will be NULL for products without images, so you can use this condition: $collection->getSelect()->where('gallery_table.value_id IS NULL') Commented Feb 20, 2016 at 15:27
  • having('COUNT(gallery_table.value_id) = 0'); might work too, but I'm not sure if you really have access to this field in the HAVING clause Commented Feb 20, 2016 at 15:30
0

As per below code we can get products having two images or more

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

foreach($collection as $product){
  $_product = $productModel->load($product->getId());
  $Images = $_product['media_gallery']['images'];
   if(count($Images) >= 2){
     /*here is product collection*/
   }
}
1
  • Loading products in a loop is very slow and I would not recommend it. Commented Feb 17, 2016 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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