How to get Quantity Order for product to display on Product Page
Question
Trying to get this to work, it is not return anything :(
$product = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToFilter('sku', $_product->getSku());
echo 'Already Bought '.(int)$product->ordered_qty; ?>
Solution
As part of the function addOrderedQty
it will only add product information for the simple product types. What you could to is rewrite this function so that it does not add the following to the join conditions.
$adapter->quoteInto('(e.type_id NOT IN (?))', $compositeTypeIds),
Then this will give you the information about all product types. Once you have this information you can simply get the first item and get the ordered_qty.
$product->getFirstItem()->getOrderedQty();
OTHER TIPS
The result of Mage::getResourceModel('reports/product_collection')
is report collection and not report object. To get information you want try this
$productReportsCollection = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToFilter('sku', $_product->getSku());
$productReport = $productReportsCollection->getFirstItem();
echo 'Already Bought ' . (int)$productReport->ordered_qty;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange