Magento add Bestseller products by date range
Here is how to get a collection of products best sold in a given date range
01.
<ul>
02.
<?php
03.
$from
=
date
(
"Y-m-d H:i:s"
, (time()-2592000));
04.
$to
=
date
(
"Y-m-d H:i:s"
, time());
05.
06.
$_productCollection
= Mage::getResourceModel(
'reports/product_collection'
)
07.
->addAttributeToSelect(
'*'
)
08.
->addOrderedQty(
$from
,
$to
, true)
09.
->addAttributeToFilter(
'status'
, 1)
10.
->setOrder(
'ordered_qty'
,
'desc'
)
11.
->getSelect()->limit(10)->query();
12.
13.
foreach
(
$_productCollection
as
$prod
) :
14.
$_product
= Mage::getModel(
'catalog/product'
);
15.
$_product
->load(
$prod
[
'entity_id'
]);
16.
(
$i
%2) ?
$even
=
"class='even-li'"
:
$even
=
""
;
17.
?>
18.
<li <?php
echo
$even
?>>
19.
<div><a href=
"<?php echo $this->getUrl($_product->getUrlPath()) ?>"
title=
"View <?php echo $_product->name ?>"
><img src=
"<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(48); ?>"
alt=
"<?php echo $_product->name ?>"
/></a>
20.
<a href=
"<?php echo $this->getUrl($_product->getUrlPath()) ?>"
title=
"View <?php echo $_product->name ?>"
><?php
echo
Mage::helper(
'core/string'
)->truncate(
$_product
->name, 50) ?></a>
21.
<p><?php
echo
Mage::helper(
'core/string'
)->truncate(
strip_tags
(
$_product
->description), 130); ?></p></div>
22.
</li>
23.
<?php
24.
endforeach
;
25.
?>
26.
</ul>