Woocommerce: Get all orders for a product

Asked
Active3 hr before
Viewed126 times

9 Answers

product
90%

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:,USAGE 1 (for a given product ID 37 and default Completed orders status):,USAGE 2 (for a given product ID 37 and some defined orders statuses):, How is the slope of the frequency response of an analog active filter defined?

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);
load more v
88%

AND order_item_meta.meta_value IN ($product_ids),What if the product ids are more than one?,What if you only want one variation of a product?

What if you only want one variation of a product?

AND order_item_meta.meta_key = '_variation_id'
AND order_item_meta.meta_value = '$variation_id'
load more v
72%

Retrieve all orders for given a product.,The product ID is stored as meta for an Order’s items; wp_posts > wp_woocommerce_order_items > wp_woocommerce_order_itemmeta. Unfortunately, this is more complicated than just using WP_Query to grab orders.,I put together a little function to return an array of WC_Order objects for a given Product ID.,The woocommerce_order_items has a reference to the Order ID with the order_id field, but thewoocommerce_order_itemmeta has a Product ID in the meta_value as a string. Joining these two tables will allow us to grab the Order ID, but since the Product ID is stored in the meta_value field, a dynamic column will have to be created to evaluate against the Product ID.

The woocommerce_order_items has a reference to the Order ID with the order_id field, but thewoocommerce_order_itemmeta has a Product ID in the meta_value as a string. Joining these two tables will allow us to grab the Order ID, but since the Product ID is stored in the meta_value field, a dynamic column will have to be created to evaluate against the Product ID.

SELECT
   `items`.
`order_id`,
MAX(CASE WHEN `itemmeta`.
   `meta_key` = '_product_id'
   THEN `itemmeta`.
   `meta_value`
   END) AS `product_id`
FROM
   `wp_woocommerce_order_items`
AS `items`
INNER JOIN
   `wp_woocommerce_order_itemmeta`
AS `itemmeta`
ON
   `items`.
`order_item_id` = `itemmeta`.
`order_item_id`
WHERE
   `items`.
`order_item_type`
IN('line_item')
AND
   `itemmeta`.
`meta_key`
IN('_product_id')
GROUP BY
   `items`.
`order_item_id`
HAVING
   `product_id` = 8 # Product ID
load more v
65%

As we’ve seen in other articles, get product info from $product object and get cart info from $cart object, not always you can have direct access to the $order variable.,How can i get product custom field from my order total for telegram notification?,WooCommerce: How To Print Orders Automatically ,Hi everyone! in section of order items

load more v
75%

Additionally, I also needed to get extra information about the actual products being ordered for each individual order, their quantity, costs associated etc.,I attempted to use plugins for this as they offered the ability to export individual orders which seemed to be exactly what I needed.,Recently, for a client site, I needed to export WooCommerce information about customers, their orders and the products that they had bought so that they could be transferred from one site to another.,This is just an alternative way of extracting WooCommerce order information straight from the database whenever a large number of orders need to be exported.

To fetch only orders from the wp_posts table, I selected posts with the post_type “shop_order”.

/* get orders from wp_posts table */
SELECT * FROM `wp_posts` WHERE post_type = "shop_order";

/* get orders from wp_posts table */
SELECT * FROM `wp_posts`
WHERE post_type = "shop_order";
load more v
40%

Copy the Order ID from the list of WooCommerce orders.,Can I list the products ordered?,This will create a table of WooCommerce orders that looks like this:,However, there are some situations where it's appropriate to list WooCommerce orders on the front end.

  1. Get the Posts Table Pro plugin.
  2. Install the plugin and activate your license key. You'll receive instructions for this in your confirmation email.
  3. Go to the page you want to list WooCommerce orders on the front end. Add the following shortcode:
[posts_table post_type = "shop_order"
   columns = "id:Order ID,date,cf:_shipping_first_name:First Name,cf:_shipping_last_name:Last Name,cf:_shipping_address_1:Address 1,cf:_shipping_city:City,cf:_shipping_postcode:Postcode,cf:_billing_phone:Phone,cf:_order_total:Total ($)"
   status = "any"
   links = "none"
]
load more v
22%

load more v
60%

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.,In the Woocommerce API I see a method wc_get_orders,Then you can embed this in a custom function with $product_id as argument. You will have to set inside it, the order statuses that you are targeting.,I need to use this method since some arguments are necessary

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);
load more v
48%

Almost all endpoints accept optional parameters which can be passed as a HTTP query string parameter, e.g. GET /orders?status=completed. All parameters are documented along each endpoint.,While redirecting the user using return_url, you are also sent success and user_id parameters as query strings.,You must use the /wc-auth/v1/authorize endpoint and pass the above parameters as a query string.,The OAuth parameters may be added as query string parameters or included in the Authorization header.

curl https: //example.com/wp-json/wc/v3/products/tags/34?_jsonp=tagDetails \
   -u consumer_key: consumer_secret
load more v

Other "product-undefined" queries related to "Woocommerce: Get all orders for a product"