2016-12-16 76 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。评论(建设性的)欢迎:)