Matthew Jason Dever
Matthew Jason Dever

Reputation: 13

Count number of orders for a variation in Woocommerce

I have figured a way to do this, but I am finding my queries are taking way too long, and the more orders in Woocmmerce, and the more variations we add, the longer the query takes...

I would hope there is a way in WC or WP to query just variation ids of an order, but alas, I havent found it yet. I need to do sales report by variation.

//get number of orders per variation_id
function getOrdersfromVariation($variation_id){
    $numberOfOrders = 0;
    ip_write_log("getOrdersfromVariation varid: $variation_id");

    // rewrite with wc_get_orders
    $args = array(
      'status' => array(  'processing', 'completed'),
        'limit' => -1,
    );
    $orders = wc_get_orders( $args );
    if(isset($orders)){
    //TODO: Get order count - $total_orders = $orders->total;

       foreach ($orders as $order){
           foreach ($order->get_items() as $key => $lineItem) {
               $item_data = $lineItem->get_data();

               if ($item_data['variation_id'] == $variation_id) {
                  $numberOfOrders++;

               }
           }
        }
        if(isset($numberOfOrders)){
           return $numberOfOrders;
        }
     }
  return;
}

Upvotes: 1

Views: 1384

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253921

You can get the count of orders from specific orders status from a variation ID with this very light SQL query embedded in the following function:

function count_orders_from_variation($variation_id){
    global $wpdb;

    // DEFINE below your orders statuses
    $statuses = array('wc-completed', 'wc-processing');

    $statuses = implode("','", $statuses);

    return $wpdb->get_var("
        SELECT count(p.ID) FROM {$wpdb->prefix}woocommerce_order_items AS woi
        JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        JOIN {$wpdb->prefix}posts AS p ON woi.order_id = p.ID
        WHERE p.post_type = 'shop_order' AND p.post_status IN ('$statuses')
        AND woim.meta_key LIKE '_variation_id' AND woim.meta_value = $variation_id
    ");
}

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

USAGE EXAMPLE (Displaying orders count for variation ID 41):

echo '<p>Orders count: ' . count_orders_from_variation(41) . '</p>';

Upvotes: 2

PPL
PPL

Reputation: 6555

You can use below code in function.php file of your active child theme (or theme) or also in any plugin file.

function get_all_orders_items_from_a_product_variation( $variation_id ){

    global $wpdb;

    // Getting all Order Items with that variation ID
    $item_ids_arr = $wpdb->get_col( $wpdb->prepare( "
        SELECT `order_item_id` 
        FROM {$wpdb->prefix}woocommerce_order_itemmeta 
        WHERE meta_key LIKE '_variation_id' 
        AND meta_value = %s
    ", $variation_id ) );

    return $item_ids_arr; // return the array of orders items ids    
}

Below code will display a list of orders items IDs for this variation ID with some data.

$items_ids = get_all_orders_items_from_a_product_variation( 41 );


foreach( $items_ids as $item_id ){


    $item_color = wc_get_order_item_meta( $item_id, 'pa_color', true );

    // Displaying some data related to the current order item
    echo 'Item ID: '. $item_id . ' with color "' . $item_color .'"<br>';
}

Hope this works for you.

Upvotes: 0

Related Questions