Vote up!
Vote down!

display number of orders


in the teaser of products I want to display the number of times the product was already bought by clients.

This number also needs to show up for anonymous users. Anonymous users can't see anything which is attached to orders.

How can I solve this? EntityFieldQuery?


Asked by: Kris Obbers
on January 8, 2014


Sorry I posted this as an answer by mistake first.

Are you talking about showing this information on the product display node? or on the product itself?

If you can provide the path to the section you're talking about I can take a look on one of my sites.

- fatguylaughing on January 10, 2014

I want to show this on the product display.
So every time someone place an order, this number should increase +1.

anonymous users can't get access to orders, but they need to see the number of orders (product popularity)

sorry, no path since dev is on localhost.


- Kris Obbers on January 16, 2014

1 Answer

Vote up!
Vote down!

You can do this via custom code using EntityFieldQuery if you want, but I actually think you can manage this via Views. If you're showing this on the product display, you can create a Views block that only appears on those pages and uses the node ID as a contextual filter. If this is over your head, you may need to research Views a bit. : )

Once you have that in place, you can join from the display node to the referenced product to line items referencing this product. With Views aggregation enabled, you can then SUM the quantity of all line items referencing that product to determine how many times it's been ordered. You'd filter this to only line items on completed orders by joining from the line item to the order table and filtering on the order status.

For an example of this sort of functionality, you can look at Commerce Reporting, which has a product report with the exact same functionality, just as a general list showing all products purchased on the site.

Ryan Szrama
Answer by: Ryan Szrama
Posted: Jun 13, 2014