Problem:
I need a way to get all sales with a specific coupon code first. Then get the total revenue from all these sales (and, preferably, subtract any profit from these sales. To get the actual amount of income).
Idea for fulfillment
I use the woocommerce 2.6.8 and MySql database for this. I assume that somehow I will have to first calculate the number of sales made using a particular coupon with PHP and MySql. Then, for each unique order ID with a specific coupon, create a new request for the total amount.
Any suggestions on how PHP looks and queries are greatly appreciated :)
I must indicate that I do not want to receive the total discount amount from the coupon . I need to calculate the total price that was sold with a particular coupon (not a discount).
OK, so there is still no working solution. But this is exactly what I consider to be the basic structure of this. Getting the total amount is not so simple because the coupon is not directly related to the order. I believe the request should have been something on the line of this:
{TBLPRFX} _woocommerce_order_items
Step 1. GET order_id FOR order_item_name = {COUPON_NAME}
Step 2. GET order_item_id FOR order_item_type = line_item WHERE order_id EQUAL {RESULT FROM STEP 1}
| order_item_id | order_item_name | order_item_type | order_id |
| 40971 | {COUPON_NAME} | coupon | 001
| 40970 | VAT | tax | 001
| 40969 | {PRODUCT_NAME} | line_item | 001
-
{TBLPRFX} _woocommerce_order_itemmeta strong>
Step 3. SUM meta_value FROM meta_key = _line_tax AND meta_key = _line_total WHERE order_item_id = {RESULT FROM STEP 2}
| order_item_id | meta_key | meta_value |
| 40969 | _line_tax | {VALUE_TAX}
| 40969 | _line_total | {VALUE_TOTAL}
| 40969 | _product_id | {PRODUCT_ID}
-
These are the queries I need to help figure out :) Not quite sure how to ask for it in MySql and PHP. The idea is to do this foreach, where "order_item_name = {COUPON_NAME_VARIABLE}", so I can summarize the total amount of all sales in which this coupon was used (i.e. Not the value of the coupon discount).