0

I need to create a list of users that have made purchases on my site. I need to be able to filter this list by product and show which users bought it. It is similar to how the Who Bought What module in Ubercart does it (but now I need it for commerce). Any thoughts are appreciated.

3
+50

Use Views! I guess you saw that coming, right? If you're in need of great examples, check out the default Views shipped with the Commerce Reports module.

In your case, I'd create a View using Commerce Order as the base entity. I'd filter it by order state or status so you only show completed orders. I'd then add relationships to the user table and to the line item table. Through the line item table, I'd add a relationship to referenced products. This will get you all the data you need in your query, and you can then add display fields pertaining to the user / order / line item info and filter based on the SKU / title of the product. You'll probably need some sort of aggregation to group multiple purchases of the same product by the same user.

That is by no means a complete recipe, but it's basically the only way you're going to solve this short of coding a custom interface and doing it all through EntityFieldQuery.

5
  • Yeah I started by modifying one of the commerce reports to meet my needs but as soon as I started adding the relationships I ended up with a ton of duplicates. Adding distinct to the view did not help. I will give a go based on your advice and see if I can get it working. Feb 28 '13 at 21:44
  • Thanks, @Ryan Szarama. I got pretty close. The last piece missing is "Through the line item table, I'd add a relationship to referenced products.". This relationship is not available for selection. I am assuming this relates to drupal.org/node/1319810 and I am left at a dead end? Mar 1 '13 at 23:02
  • Nope, this relationship works right now. Assuming you've added the Commerce Order: Referenced line item relationship, you should be able to add the Commerce Line item: Referenced product. I see it on a clean install of Commerce 1.5; that patch you've pointed to is for other relationships. Mar 3 '13 at 5:53
  • Ok. I scrapped trying to use the views provided by commerce reports and just went with a clean view. Thanks for sticking with me on this @Ryan Szrama. I was finally able to get it done with the following...Use the base table Orders then add the following relationships: Commerce Order: Owner > Commerce Order: Referenced line item > Commerce Line item: Referenced product Mar 3 '13 at 18:52
  • Awesome, glad to hear you got it working! Mar 3 '13 at 20:50

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.