2

I came across these: How to get WooCommerce order details

and was wondering if these commands are powerful enough to work like mysql commands where you can SUM(quantity) of a product purchased and then group that by userid ... ?

so say I had this table in my database of orders;

user | product | quantity
1 | chair | 4
2 | chair | 4
1 | chair | 6
2 | bench | 3
2 | bench | 2

would I be able to use those commands presented in the link above to get this?

user | product | quantity
1 | chair | 10
2 | chair | 4
2 | bench | 5

and if so ... how?? I wanna collate all the data in my orders by total quantity per product purchased per user in a php based table...

Thanks :)

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
NRav
  • 407
  • 1
  • 6
  • 18

1 Answers1

1

For your example it seems that using a SQL query is still the most effective way, even if you could use a the WC_Order_Query. Developers still use a lot SQL queries when it's necessary, lighter and more accurate. The WPDB Wordpress Class is the way to make that.

Additionally, there is a lot of Woocommerce specific built in functions that can be used too (and some from Wordpress too).

All new Woocommerce CRUD setters and getter methods are very powerful but used for example to get specific data from an order or to make changes programmatically in an existing Order, re-calculate totals and save it to the database.

For CRUD methods around the order object, you can take a look to the source code in WC_Checkout for the create_order() method that will show you that those methods are used to create a complete order. As you will see at the end, the method save() will register all the data in the database.

To resume: There is no an unique way, but many, depending on what you want to do, on the time to be spent and on your skills. For you the way here is a SQL query.

For the "how to do it?" You already asked that in your other related question and Gordon Linoff (a top rated user for SQL and mySQL) can really help you with that complex SQL query.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you for your response! it looks like a SQL query is my best option... Just gotta figure out how to format the query. Still trying to to grasp SQL queries from multiple tables! – NRav Jun 10 '18 at 03:46