Home » Php » php – Woocommerce: Get all orders for a product

php – Woocommerce: Get all orders for a product

Posted by: admin July 12, 2020 Leave a comment

Questions:

In the Woocommerce API I see a method wc_get_orders

WooCommerce Order Functions

In the mentioned args I do not see an argument in which I could provide a product ID or Object to limit order results only for that specific product.

Is there a way I could filter orders only for specific products?

I need to use this method since some arguments are necessary

How to&Answers:

Edited
This function doesn’t exist, but it can be built. So the function below will return an array of all orders IDs for a given product ID, making the right SQL query:

/**
 * Get All orders IDs for a given product ID.
 *
 * @param  integer  $product_id (required)
 * @param  array    $order_status (optional) Default is 'wc-completed'
 *
 * @return array
 */
function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ){
    global $wpdb;

    $results = $wpdb->get_col("
        SELECT order_items.order_id
        FROM {$wpdb->prefix}woocommerce_order_items as order_items
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
        LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
        WHERE posts.post_type = 'shop_order'
        AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
        AND order_items.order_item_type = 'line_item'
        AND order_item_meta.meta_key = '_product_id'
        AND order_item_meta.meta_value = '$product_id'
    ");

    return $results;
}

USAGE 1 (for a given product ID 37 and default Completed orders status):

$orders_ids = get_orders_ids_by_product_id( 37 );

// The output (for testing)
print_r( $orders_ids );

USAGE 2 (for a given product ID 37 and some defined orders statuses):

// Set the orders statuses
$statuses = array( 'wc-completed', 'wc-processing', 'wc-on-hold' );

$orders_ids = get_orders_ids_by_product_id( 37, $statuses );

// The output (for testing)
print_r( $orders_ids );

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

Answer:

So I wanted to fetch orders specific to a product and a person. I used the solution provided by @LoicTheActec with the wc_get_orders woocommerce method to get the result I want.

Here are the functions:

    /**
 * Get All orders IDs for a given product ID.
 *
 * @param  integer  $product_id (required)
 * @param  array    $order_status (optional) Default is 'wc-completed'
 *
 * @return array
 */
public function get_orders_ids_by_product_id( $product_id, $order_status = array( 'wc-completed' ) ) {
    global $wpdb;

    $results = $wpdb->get_col("
        SELECT order_items.order_id
        FROM {$wpdb->prefix}woocommerce_order_items as order_items
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
        LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
        WHERE posts.post_type = 'shop_order'
        AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
        AND order_items.order_item_type = 'line_item'
        AND order_item_meta.meta_key = '_product_id'
        AND order_item_meta.meta_value = '$product_id'
    ");

    return $results;
}

To filter out the results I want. Since the method does not provide an include argument I had to grab all the orders and then remove the order for the given product id and exclude all the remaining from wc_get_orders:

    /**
 * Check if current user has more than 3 orders
 *
 * @return void
 * @author 
 **/
public function woocommerce_check_order_count($product_id, $customer_email)
{   


    $statuses = array(  'wc-processing' );

    $orders_ids_for_product = $this->get_orders_ids_by_product_id( $product_id, $statuses );


    // Get All Orders
    $allorders = wc_get_orders( array(
        'status'      => 'processing',
        'type'        => 'shop_order',
        'email'       => '',
        'limit'       => -1,
        'return'      => 'ids',
    ) );

    // Remove the $orders_ids_for_product array from the $allorders array
    $orderstoexclude = array_diff($allorders, $orders_ids_for_product);

    $orders = wc_get_orders( array(
        'status'      => 'processing',
        'type'        => 'shop_order',
        'customer'    => $customer_email,
        'email'       => '',
        'limit'       => 3,
        'exclude'     => $orderstoexclude,
        'return'      => 'ids',
    ) );

    return $orders;

}