To get Recently Sold Products

Posted: November 7, 2012 in Magento, Version 1.3.*, Version 1.4.*, Version 1.6.*

Hello All,

I like to share small code snippet to last 12 product details which are recently sold from the store.

public function getRecentlySoldItems()
{
$storeID = Mage::app()->getStore()->getId();
$itemsCollection = Mage::getResourceModel('sales/order_item_collection')
->join('order', 'order_id=entity_id')
->addFieldToFilter('main_table.store_id', array('eq'=>$storeID))
->setOrder('main_table.created_at','desc')
->setPageSize(12);
$itemsCollection->getSelect()->group(`main_table`.'product_id');
$products = array();
if(sizeof($itemsCollection)>0)
{
foreach ($itemsCollection as $item) {
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($item->getProductId());

$products[] = $product;
}
}

return $products;

}

Above function will return array of products.

I hope you will find the above code useful.

Thanks
Bijal Bhavsar 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s