WooCommerce 访问销售产品的订单 [英] WooCommerce access orders that have products on sale

查看:83
本文介绍了WooCommerce 访问销售产品的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,我有这个函数可以遍历所有订单并获取有优惠券折扣的订单:

In WooCommerce, I have this function that loops through all the orders and get the orders that have coupon discounts:

$orders = wc_get_orders( array('numberposts' => -1) );

// Loop through each WC_Order Object
foreach( $orders as $order ){
    if ( sizeof($order->get_used_coupons()) > 0 ) {
        $order_data = $order->get_data(); // The Order data
        $data = 'Order Number: #' . $order->id . '<br>' .'Order Status: '. $order->status . '<br>' . 'Order Creation Date: ' . $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s') . '<br>' . 'Order Total: '. $order->total . '<br>' . 'Customer Username: ' . $order_billing_first_name = $order_data['billing']['first_name'] . '<br>' . 'Customer E-Mail: '. $order_billing_email = $order_data['billing']['email'] . '<br>' . 'Customer Phone: ' . $order_billing_phone = $order_data['billing']['phone'] . '<br>'; 
    }

}  

不是优惠券,是否有可能获得所有有特价商品的订单?我该怎么做?

Instead of coupons, Is it possible to get all orders that have products on sale? How I can do that?

推荐答案

更新:以下内容将让您获得包含特价"商品的 WooCommerce 订单.

Updated: The following will let you get WooCommerce orders that have items "on sale".

具有自定义 SQL 查询的函数,该函数可获取包含在售"商品的订单 ID:

A function with a custom SQL query that get Orders Ids with "on sale" items:

// Get Orders Ids with "on sale" items
function wc_get_onsale_items_order_ids() {
    global $wpdb;

    // The paid order statuses
    $statuses = implode( "','wc-", array_map( 'esc_sql', wc_get_is_paid_statuses() ) );

    // The_query
    return $wpdb->get_col("
        SELECT DISTINCT o.ID
        FROM {$wpdb->prefix}posts o
        INNER JOIN {$wpdb->prefix}postmeta om
            ON o.ID = om.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items oi
            ON o.ID = oi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim
            ON oi.order_item_id = oim.order_item_id
        WHERE o.post_type = 'shop_order'
        AND o.post_status IN ('wc-$statuses')
        AND oim.meta_key IN ('_product_id','_variation_id')
        AND oim.meta_value IN (
            SELECT p.ID
            FROM {$wpdb->prefix}posts p
            INNER JOIN {$wpdb->prefix}postmeta pm
                ON p.ID = pm.post_id
            WHERE p.post_type IN ('product','product_variation')
            AND p.post_status = 'publish'
            AND pm.meta_key = '_sale_price'
            AND pm.meta_value != ''
            AND pm.meta_value > 0
        )
    ");
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

代码中的用法:

// Loop through each WC_Order Object
foreach( wc_get_onsale_items_order_ids() as $order_id ){
    // Get The WC_Order Object
    $order = wc_get_order( $order_id );

    // The dormatted data
    $data = 'Order Number: #' . $order->get_order_number() . '<br>' .
        'Order Status: '. $order->get_status() . '<br>' . 
        'Order Creation Date: ' . $order->get_date_created()->date('Y-m-d H:i:s') . '<br>' . 
        'Order Total: '. $order->get_total() . '<br>' . 
        'Customer Username: ' . $order->get_billing_first_name() . '<br>' . 
        'Customer E-Mail: '.  $order->get_billing_email() . '<br>' . 
        'Customer Phone: ' . $order->get_billing_phone(); 
    }
}  

这篇关于WooCommerce 访问销售产品的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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