简体   繁体   中英

Woocommerce total sales value from specific coupon

The issue:

I need a way to first get all sales made with a specific coupon code. Then get the total revenue from all those sales (and preferably subtracting any returns on those sales. To get the actual revenue value).

Idea for executing

I'm using woocommerce 2.6.8 and MySql database for this. My guess is that I would somehow have to first count the number of sales made with a specific coupon with PHP and MySql. Then, for each unique order id with the specific coupon, make a new query for the sum total.

Any suggestions on how the PHP and queries would look like are greatly appreciated :)

I should point out that I'm not looking to get the sum total of the discount from the coupon . I need to calculate the total value that has been sold with the specific coupon (not the discount).

OK, so still no working solution thus far. But this is what I believe is the basic structure of it. Getting the total isn't that easy because the coupon isn't directly associated with the order. I believe that the query would have to be something in 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

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}

--

It's the queries I need help figuring out :) Not really sure how to ask for this in MySql and PHP. The idea is to make this a foreach where "order_item_name={COUPON_NAME_VARIABLE}", so I can sum up the total from all sales where that coupon was used (ie NOT the coupon discount value).

So after a lot of testing I came up with a working solution. It's not the sleekest one, but gets the job done.

If any SQL-ninjas see this it would be great if you could suggest any more streamlined queries :)

After making the first working solution I realized that there are more steps that need to be checked for a more accurate total. Handling refunds, taxes, discounts etc. and also make sure I only get the data from completed orders (since canceled, on-hold, pending etc isn't a sale until it's completed).

So, this is what I ended up with. Like I said, I know it needs some work and could probably be done better. But for now, it works.

  • REVISED CODE FOR FUNCTIONS.PHP -

     // COUPON CHECK function couponcheck() { global $wpdb; $gtl = 0; // Grand Total $total_sales = 0; $cpn = $_GET['cpn']; // Get coupon name from URL string $tblprfx = '[YOUR_TABLE_PREFIX]'; // Table prefix $wpps = 'posts'; // Look for post_status $wppm = 'postmeta'; $wcoi = 'woocommerce_order_items'; $conversion_value = 1; $base_currency = '[YOUR_BASE_CURRENCY]'; $currency; $orders_made; // Check to make sure there is a couon name in string if(isset($cpn) && !empty($cpn)){ // Query chain $init_result = $wpdb->get_results("SELECT order_id FROM {$tblprfx}{$wcoi} WHERE order_item_name='$cpn'"); $orders_made = count($init_result); foreach($init_result as $ir){ $r1 = $ir->order_id; $completed_result = $wpdb->get_results( "SELECT ID FROM {$tblprfx}{$wpps} WHERE post_status='wc-completed' AND ID=$r1" ); foreach($completed_result as $post_rows) { $pr = $post_rows->ID; $completed_sales += 1; $currency_result = $wpdb->get_results( "SELECT meta_value FROM {$tblprfx}{$wppm} WHERE post_id=$pr AND meta_key='_order_currency'" ); foreach($currency_result as $cr) { $currency = $cr->meta_value; if($currency === 'EUR'){ $currency = 'EUR'; $currency_rate = $wpdb->get_results( "SELECT meta_value FROM {$tblprfx}{$wppm} WHERE post_id=$pr AND meta_key='_woocs_order_rate'" ); foreach($currency_rate as $rv) { $rate_value = $rv->meta_value; $conversion_value = $rate_value; } } } $data_result = $wpdb->get_results( "SELECT meta_value FROM {$tblprfx}{$wppm} WHERE post_id=$pr AND meta_key='_order_total'" ); foreach($data_result as $dr) { $d = $dr->meta_value; if($currency === 'EUR'){ $eur += $d; $d = $d/$conversion_value; }else{ $[YOUR_BASE_CURRENCY] += $d; } $gtl += $d; } } } // Total number of sales $refunded_orders = $orders_made-$completed_sales; $order_avg = $gtl/$completed_sales; echo '<p>Total <strong>completed, non-refunded, sales made (after discounts)</strong> with coupon <strong>' . strtoupper($cpn) . '</strong>: <br />(Number of refunded orders: <strong>' . $refunded_orders . ')</strong></p><h2>' . $completed_sales . '</h2><p>At a total <strong>sales value</strong> of:</p><h2>' . number_format($[YOUR_BASE_CURRENCY],2) . ' [YOUR_BASE_CURRENCY]</h2><h2>&euro;' . number_format($eur,2) . '</h2><p>Adding up to a <strong>sum total</strong> of:</p><h2>' . number_format($gtl,2) . ' [YOUR_BASE_CURRENCY]</h2><p>Creating an average order value of:<br /><strong>' . number_format($order_avg,2) . ' [YOUR_BASE_CURRENCY]</strong>'; } } add_shortcode('coupons', 'couponcheck'); 

A small note is that I changed my actual base currency to [YOUR_BASE_CURRENCY]. Comments (constructive ones) are welcome :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM