Get best selling or hot selling products list
-
16-10-2019 - |
문제
I am new to PHP. I am trying to get best selling products list using the code which I found in Mage_Adminhtml_Block_Dashboard_Tab_Products_Ordered
class from _prepareCollection
method.
if ($this->getParam('website')) {
$storeIds = Mage::app()->getWebsite($this->getParam('website'))->getStoreIds();
$storeId = array_pop($storeIds);
} else if ($this->getParam('group')) {
$storeIds = Mage::app()->getGroup($this->getParam('group'))->getStoreIds();
$storeId = array_pop($storeIds);
} else {
$storeId = (int)$this->getParam('store');
}
$_productCollection = Mage::getResourceModel('sales/report_bestsellers_collection')
->setModel('catalog/product')
;
?>
But I can't get the best selling products list. I am getting something else(very big dump text) when I checked using
<?php echo Zend_Debug::dump($_productCollection); ?>
PS: $_productCollection is a variable which I suppose to store a array of products so that I can use it in foreach loop later.
해결책
you can iterate through $_productCollection
and get the products.
foreach ($_productCollection as $product){
//do something with $product
//for example
//Zend_Debug::dump($product->getData());
}
[EDIT]
Each product in the collection is not actually a product. It only contains some details about the ordered qty.
If you call getData
on a product you get something like this:
Array
(
[period] => 2013-01-01 // probably January 1st current year
[qty_ordered] => 2.0000// qty ordered
[product_id] => 176 // id of product
[product_name] => PRODUCT NAME HERE
[product_price] => 44.99 //product price here
)
To get the full product you may need to do this :
foreach ($_productCollection as $product){
$p = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($product->getProductId());
//attach the ordered qty
$p->setQtyOrdered($product->getQtyOrdered());
//do something with $p
//for example
//Zend_Debug::dump($p->getData());
}
I know that it's not really OK to use load
in a loop. It may cause performance issues, but if you don't have many products you should be OK.