WooCommerce access orders that have products on sa

2019-07-27 21:27发布

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?

1条回答
别忘想泡老子
2楼-- · 2019-07-27 22:14

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

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
        )
    ");
}

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


USAGE in your code:

// 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(); 
    }
}  
查看更多
登录 后发表回答