I want to get the ordered quantity of a product. I can get sku, name, price, etc. But I can't get the ordered quantity. This is what I am so far:
public function render(Varien_Object $row)
{
$sku = Mage::getModel("catalog/product")->load($row->getId())->getSku();
$price = Mage::getModel("catalog/product")->load($row->getId())->getPrice();
$quantity = Mage::getModel("catalog/product")->load($row->getId())->getQtyOrdered();
}
Please help!
Here's how you could do that :
$orderedQty = 0;
$report = Mage::getResourceModel('reports/product_sold_collection')
->addOrderedQty() //you can also filter by date range, like ->addOrderedQty($from, $to) with proper SQL dates
->addIdFilter($row->getId())
->getFirstItem();
if ($report) {
$orderedQty = $report->getOrderedQty();
}
Additionally, you might want to dive in the classes Mage_Reports_Model_Resource_Product_Sold_Collection
and Mage_Reports_Model_Resource_Product_Collection
in which you'll find some interesting self-explanatory methods such as :
public function setDateRange($from, $to)
public function setStoreIds($storeIds)
public function addOrdersCount($from = '', $to = '')