如何检索使用特定优惠券的WooCommerce订单列表? [英] How to retrieve a list of WooCommerce orders which use a particular coupon?

查看:147
本文介绍了如何检索使用特定优惠券的WooCommerce订单列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,该函数检索使用指定优惠券代码且在指定日期范围内的WooCommerce订单列表,然后求和应用于这些订单的总折扣.

I'm trying to write a function that retrieves a list of WooCommerce orders which use a specified coupon code and are in a specified date range, then sums the total discounts applied to those orders.

经过一番谷歌搜索后,我觉得我应该使用类似的东西

After a little googling I feel like I should be using something like

$customer_orders = get_posts( array(
    'numberposts' => -1,
    'meta_key'    => ???,
    'meta_value'  => $CouponToSearchFor,
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_order_statuses() ),
) ); 

我尝试过:

'meta_key' => 'coupon'
'meta_key' => 'shop_coupon'
'meta_key' => '_coupon'

但是这些都不起作用.如何找出哪些meta_key/meta_value术语会满足我的需求?

But none of those work. How can I find out which meta_key/meta_value terms will give me what I need?

另外,我认为meta_query可以作为此get_posts()查询的一部分用于执行日期过滤,对吗?

Additionally I think meta_query can be used to perform the date filtering as part of this get_posts() query, is that correct?

推荐答案

您的代码不起作用,因为默认情况下WooCommerce不存储 wp_postmeta表中使用的优惠券代码.它存储在 wp_woocommerce_order_items表,在 order_item_type => coupon order_item_name => YOUR_CODE 下.

Your code is not working because by default WooCommerce does't store used coupon code in wp_postmeta table. It stores in wp_woocommerce_order_items table, under order_item_type => coupon and order_item_name => YOUR_CODE.

您必须首先获取所有订单ID,然后循环播放以获取所需的总金额,税项或折扣.

You have to first get all the Order ID(s), then you have to loop it to get the desired total, or tax or discount.

这是代码:

function wh_getOrderbyCouponCode($coupon_code, $start_date, $end_date) {
    global $wpdb;
    $return_array = [];
    $total_discount = 0;

    $query = "SELECT
        p.ID AS order_id
        FROM
        {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        WHERE
        p.post_type = 'shop_order' AND
        p.post_status IN ('" . implode("','", array_keys(wc_get_order_statuses())) . "') AND
        woi.order_item_type = 'coupon' AND
        woi.order_item_name = '" . $coupon_code . "' AND
        DATE(p.post_date) BETWEEN '" . $start_date . "' AND '" . $end_date . "';";

    $orders = $wpdb->get_results($query);

    if (!empty($orders)) {
        $dp = ( isset($filter['dp']) ? intval($filter['dp']) : 2 );
        //looping throught all the order_id
        foreach ($orders as $key => $order) {
            $order_id = $order->order_id;
            //getting order object
            $objOrder = wc_get_order($order_id);

            $return_array[$key]['order_id'] = $order_id;
            $return_array[$key]['total'] = wc_format_decimal($objOrder->get_total(), $dp);
            $return_array[$key]['total_discount'] = wc_format_decimal($objOrder->get_total_discount(), $dp);
            $total_discount += $return_array[$key]['total_discount'];
        }
//        echo '<pre>';
//        print_r($return_array);
    }
    $return_array['full_discount'] = $total_discount;
    return $return_array;
}

代码进入活动子主题(或主题)的function.php文件中.或在任何插件php文件中.

用法

$orders = wh_getOrderbyCouponCode('my_code', '2016-09-17', '2016-10-07');
echo 'Total Discount : ' . $orders['full_discount'];
//print_r($orders);

请注意:

所有日期均为YYYY-MM-DD格式.
print_r(array_keys(wc_get_order_statuses()));将输出如下内容:

All dates are in YYYY-MM-DD format.
print_r(array_keys(wc_get_order_statuses())); will output something like this:

Array
(
    [0] => wc-pending
    [1] => wc-processing
    [4] => wc-on-hold
    [5] => wc-completed
    [6] => wc-cancelled
    [7] => wc-refunded
    [8] => wc-failed
)

代码已经过测试并且可以正常工作.

希望这会有所帮助!

这篇关于如何检索使用特定优惠券的WooCommerce订单列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆