0

I want to display the number of purchased item/s on a certain product. I did use this code:

$resultQuantity = $this->getQuantityPurchase($_product);
$resultQuantity[0];

But it displays nothing. What should I do? Could I use the Product ID for this?

2 Answers 2

1
$id = 123; // enter your product ID here
$product = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToFilter('sku', $produt->getSku())
    ->setOrder('ordered_qty', 'desc')
    ->getFirstItem();

echo (int)$product->ordered_qty;
7
  • I have tried your code and I got same results for all products.
    – Alen
    Commented Jan 15, 2016 at 6:52
  • update your tried code in question? Commented Jan 15, 2016 at 6:53
  • yes I did update it.
    – Alen
    Commented Jan 15, 2016 at 6:54
  • updated answer and tested too.. Commented Jan 15, 2016 at 7:04
  • it is working but it includes the one which are pending for payment.
    – Alen
    Commented Jan 15, 2016 at 7:32
1

You can do this by getting the product sold collection

/** @var Mage_Report_Model_Mysql4_Product_Sold_Collection **/
$report = Mage::getResourceModel('reports/product_sold_collection');

You can set a date range or other criteria if you want. Once you have your collection just iterate over it, something like this:

foreach ($report AS $product) {
echo $product->getOrderedQty();}
6
  • I am not familiar with Mage::getResourceModel('report/product_sold_collection'). What are the filter that can be used on this report?
    – Alen
    Commented Jan 15, 2016 at 9:50
  • you can use product attribute filters like $report->addAttributeToFilter('sku', $produt->getSku()) Commented Jan 15, 2016 at 10:05
  • I see. I will give it a try.
    – Alen
    Commented Jan 15, 2016 at 10:13
  • I am getting an error regarding the Filter.
    – Alen
    Commented Jan 15, 2016 at 10:20
  • What type of error your are getting on apply filter.you can set date range like $report->setDateRange($from, $to) Commented Jan 15, 2016 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.