2
$store = Mage::app()->getStore();
$products = Mage::getResourceModel('reports/product_collection')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter("status", Mage_Catalog_Model_Product_Status::STATUS_ENABLED)     
                 ->addPriceData() 
                ->addOrderedQty()
                ->setOrder("ordered_qty", "desc")
                ->setStore($store)
                ->addStoreFilter($store);

I used this product collection but when I use foreach for this collection I can not fetch all product attributes like(small_image).I know one solution in foreach $product=Mage::getModel('catalog/product')->load($_product->getId()); But I do not want to use load in foreach loop. Any alternative soltution ?

1
  • have my answer helped you ? Commented Jul 11, 2016 at 5:55

2 Answers 2

4
$store = Mage::app()->getStore();
$products = Mage::getResourceModel('reports/product_collection')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter("status", Mage_Catalog_Model_Product_Status::STATUS_ENABLED)     
                ->addPriceData() 
                ->addOrderedQty()
                ->setOrder("ordered_qty", "desc")
                ->setStore($store)
                ->addStoreFilter($store);

$productIds = $products->getColumnValues('entity_id');

$productsCollection = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect(array('name','description','price','small_image')) // add whatever product attributes you want
                        ->addAttributeToFilter('entity_id',array('in' => $productIds));

foreach($productsCollection as $product){
    echo $product->getName();
    echo $product->getDescription();
    echo $product->getPrice();
    echo Mage::helper('catalog/image')->init($product,'small_image');
}
9
  • Yes helped,without using product collection possible in report collection ? because product collection and report collection same class extend for addAttributeToSelect() method so. Commented Jul 11, 2016 at 6:01
  • Problem is all products list not order_qty descending order in page Commented Jul 11, 2016 at 6:02
  • Hello Pradeep are u there Commented Jul 11, 2016 at 6:10
  • yes say........ ? Commented Jul 11, 2016 at 6:12
  • one confusion.without using product collection possible in report collection ? because product collection and report collection same class extend for addAttributeToSelect() method so Commented Jul 11, 2016 at 6:20
0

Could not find any fast solution within reasonable time. So I am accessing the DB almost directly. load($productId, array($attributeCode)) loads all the product information For other solutions I run out of memory on my local machine.

/*
  get product attribute integer value
*/
function getProductAttributeInt($productId, $attributeCode){
    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');
    return  
      $readConnection->fetchOne(
        'select value from catalog_product_entity_int where entity_id=' . $productId . '
         and attribute_id = (select attribute_id from eav_attribute where attribute_code = \'' . $attributeCode . '\')'
      );
}

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.