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
    Jan 15, 2016 at 6:52
  • update your tried code in question? Jan 15, 2016 at 6:53
  • yes I did update it.
    – Alen
    Jan 15, 2016 at 6:54
  • updated answer and tested too.. Jan 15, 2016 at 7:04
  • it is working but it includes the one which are pending for payment.
    – Alen
    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
    Jan 15, 2016 at 9:50
  • you can use product attribute filters like $report->addAttributeToFilter('sku', $produt->getSku()) Jan 15, 2016 at 10:05
  • I see. I will give it a try.
    – Alen
    Jan 15, 2016 at 10:13
  • I am getting an error regarding the Filter.
    – Alen
    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) Jan 15, 2016 at 10:24

Your Answer

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

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