3

Sorry for my English

I have this code

$productCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('short_description')
        ->addAttributeToFilter('attribute_set_id', array(12, 4))
        ->joinField(
            'qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left'
        )
        ->addAttributeToFilter('qty', array("gt" => 0))
        ->setPageSize(100);

how can i get the number of images of this product or just get the product images. I would like to avoid the situation receiving pictures when the loop is running because it is database load

0

Add just this code in your collection.

 ->joinField(
            'count_images',
            'catalog_product_entity_media_gallery',
            'count(value)',
            'entity_id=entity_id',
            '{{table}}.attribute_id='.$your_attribute,
             'left'
        )

add this code if count the amount of each product

->groupByAttribute('your attribute')
0

You got a few more options

If you want main image url

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

foreach ($collection as $product) {
   var_dump($product->getImageUrl());
}

Or this. Or did you mean avoid doing this? Not sure. It will be extra load yes but you need the galley info so additional load is necessary.

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

$backendModel = $collection->getResource()->getAttribute('media_gallery')->getBackend();

foreach($collection as $product){
    $backendModel->afterLoad($product); //adding media gallery to the product object
    var_dump($product->getData()); 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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