2016-12-16 48 views
6

問題:Woocommerce銷售總額從具體的優惠券值

我需要一種方法首先得到一個特定的優惠券代碼所做的所有銷售。然後從所有這些銷售中獲得總收入(最好減去這些銷售的任何回報,以獲得實際收入價值)。

理念執行

我使用woocommerce 2.6.8和MySql數據庫這一點。我的猜測是,我不知何故必須首先計算使用PHP和MySql的特定優惠券的銷售數量。然後,對於具有特定優惠券的每個唯一訂單ID,對總和進行重新查詢。

在PHP和查詢如何將看起來像任何建議都非常讚賞:)

我要指出的是我不希望從優惠券得到的總和的折扣。我需要計算與特定優惠券一起銷售的總價值(而不是折扣)。

好的,到目前爲止還沒有工作解決方案。但我相信這就是它的基本結構。獲得總額並不容易,因爲優惠券並不直接與訂單相關聯。 我相信,查詢必須是東西的這行:

{} TBLPRFX _woocommerce_order_items

第1步:GET ORDER_ID FOR order_item_name = {} COUPON_NAME

第2步:GET order_item_id FOR order_item_type = line_item WHERE order_id EQUAL {來自第1步的結果}

| order_item_id | order_item_name | order_item_type | order_id |

| 40971 | {COUPON_NAME} |優惠券| 001

| 40970 |增值稅|稅| | 001

| 40969 | {PRODUCT_NAME} | line_item | 001

-

{TBLPRFX} _woocommerce_order_itemmeta

步驟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

-

這是我需要幫助搞清楚:)不是真的知道如何在MySQL和PHP詢問此查詢。這個想法是讓這個foreach在「order_item_name = {COUPON_NAME_VARIABLE}」的位置,所以我可以總結所有使用該優惠券的銷售總額(即不是優惠券折扣值)。

+0

這是一次性查詢,或者您試圖將其作爲報告添加到頁面中?有些具有woocommerce表格知識的人可能會告訴你一個簡單的SQL查詢來獲得你的結果。這是我的谷歌搜索開始研究https://www.google.com/search?q=woocommerce+sql+query+orders+with+coupon&ie=utf-8&oe=utf-8但是,您的SQL查詢可能類似於' SELECT SUM(orderValue)FROM orders WHERE order.coupon ='somecoupon'' ---所以你不必做任何計數或循環,你可以讓SQL處理繁重的工作。 –

+0

將其添加爲報告頁面可能是未來的項目。現在,我只需在PHP頁面上獲得總和就可以了:)我會看看你的GSearch,看看我能不能找出一些東西......「直到那時,請保留你的建議;)是的,你的建議查詢是我想到的東西,我只是不知道如何執行它(以及在哪裏查找所有要查找的列)。Woocommerce始終不是「非常直接」的SQL結構) – axelra82

+0

是的,很抱歉,我沒有使用wordpress或woocommerce,但是我處理數據庫,並且通常使用'admin'工具來查看SQL表格,比如excel,然後從那裏建立SQL查詢 - 我不'不知道wordpress是否有類似的東西。有時候這些工具也能夠爲所有表格創建一個很好的「網絡」,以便您可以看到它們是如何相互關聯的。 –

回答

0

因此經過很多測試後,我想出了一個工作解決方案。這不是最漂亮的,但完成了工作。

如果有任何SQL-忍者看到這一點,將是巨大的,如果你能提出任何更精簡查詢:)

使我意識到有一些需要檢查的越多步驟的第一個工作日的解決方案後準確的總數。處理退款,稅款,折扣等,並確保我只從完成的訂單中獲得數據(因爲取消,等待,等待等待完成之前不是銷售)。

所以,這就是我最終的結果。就像我說的,我知道它需要一些工作,可能會做得更好。但現在,它的工作。

  • 修訂的functions.php CODE -

    // 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'); 
    

一張小紙條,是我改變了我的實際基礎貨幣爲[YOUR_BASE_CURRENCY。評論(建設性的)歡迎:)