2

I am in product view page, and I want to display the number of customers that bought that particular product.

2 Answers 2

2

If you want to know how many different customers bought an item, you can try this (guest orders are summed as one customer):

# $product = Mage::registry('current_product');
$product = Mage::getModel('catalog/product')->load(450);
$orders = Mage::getResourceModel('sales/order_item_collection')
    ->addFieldToFilter('sku', $product->getSku());
$orderIds = $orders->getColumnValues('order_id');

$collection = Mage::getResourceModel('sales/order_collection')
    ->addFieldToFilter('entity_id', array('in' => $orderIds));

$countByDifferntCustomers = count(array_count_values(array_map(function($v) {
    return (is_null($v)) ? "?" : $v;
}, $collection->getColumnValues('customer_id'))));

if ($countByDifferntCustomers) {
    echo $countByDifferntCustomers . ' customers bought this product';
}
3
  • Thanks its working fine!!! evry time you come to help me...Thanx @sv3n Jul 1, 2017 at 11:10
  • I also make condition here, if that product is already brought by someone. If not, then dont show this text. How could i achieve this? Jul 1, 2017 at 11:14
  • 1
    Updated answer.
    – sv3n
    Jul 1, 2017 at 11:17
0
$productId = 123; // enter your product ID here
$product = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToFilter('id', $productId)
    ->setOrder('ordered_qty', 'desc')
    ->getFirstItem();

echo 'Total number of brought '.(int)$product->ordered_qty;

Let me know if it does not work for you.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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