Hi guys I am currently in magento 1.9.2.4 using the following code to obtain bestselling products, but I don't think it is the optimal way. How could I improve the code ?
<?php
class Mage_Catalog_Block_Product_Bestseller extends Mage_Core_Block_Template{
public function getBestsellerProducts()
{
$storeId = (int) Mage::app()->getStore()->getId();
// Date
$date = new Zend_Date();
$toDate = $date->setDay(1)->getDate()->get('Y-MM-dd');
$fromDate = $date->subMonth(1)->getDate()->get('Y-MM-dd');
//var_dump("to date is: ".$toDate.". From date is: ".$fromDate); die;
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addStoreFilter()
->addPriceData()
->addTaxPercents()
->addUrlRewrite()
->setPageSize(20);
$collection->getSelect()
->joinLeft(
array('aggregation' => $collection->getResource()->getTable('sales/bestsellers_aggregated_monthly')),
"e.entity_id = aggregation.product_id AND aggregation.store_id={$storeId} AND aggregation.period BETWEEN '{$fromDate}' AND '{$toDate}'",
array('SUM(aggregation.qty_ordered) AS sold_quantity')
)
->group('e.entity_id')
->order(array('sold_quantity DESC', 'e.created_at'));
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
return $collection;
}
}
?>