WP_Query and WooCommerce orders with pending status issue - php

I can't get order objects with status of wc-pending / Pending Payment. It simply returns ALL the order objects:
$my_course_query = new WP_Query( array(
'post_type' => 'shop_order',
'post_status' => 'wc-pending',
'posts_per_page' => -1
) );

Your code Is just working perfectly as expected, in frontend, I have test it and it output only orders with **pending status. So I can't tell what is your issue as your question is not detailed.
I have found this note on WordPress WP_Query reference that could be useful:
Note: Ticket #18408 For querying posts in the admin, consider using get_posts() as wp_reset_postdata() might not behave as expected.
In general, I don't use WP_Query() for customer orders but wc_get_orders() (or get_posts() too) this way:
$customer_orders = wc_get_orders( array(
'limit' => -1,
'status' => 'pending'
) );
// Iterating through each Order with pending status
foreach ( $customer_orders as $order ) {
// Going through each current customer order items
foreach($order->get_items() as $item_id => $item_values){
$product_id = $item_values['product_id']; // product ID
// Order Item meta data
$item_meta_data = wc_get_order_item_meta( $item_id );
// Some output
echo '<p>Line total for '.wc_get_order_item_meta( $item_id, '_line_total', true ).'</p><br>';
}
}
This works also just to get the orders objects.
Related documentation: wc_get_orders and WC_Order_Query

I fixed this weird issue by simply using custom query.
Somehow adding 'post_status' => 'wc-pending' doesn't actually change the query, but if I use 'post_status' => 'pending', the query changes.
So what I did was using that custom query and modify pending to wc-pending.

I do have the same issue (returning ALL Orders) while debugging.
Wrapping the debug-code into an action helped outputting the expected data:
add_action( 'init', 'debug_init' );
function debug_init() {
$custom_query_args = array(
"fields" => "ids",
"post_type" => "shop_order",
"post_status" => array('wc-processing'),
"posts_per_page" => "-1",
"offset" => "0",
"date_query" => [
"before" => "2020-09-10 23:59",
"after" => "1970-01-01 00:00",
"inclusive" => "1"
],
"order" => "DESC"
);
$debugQuery = new WP_Query( $custom_query_args );
$order_ids = $debugQuery->posts;
print_r($order_ids);
die();
}

Related

Code to display all successful purchases in WooCommerce

I want to display the total number of purchases made on my site to my site users; Previously, I wrote the code that displayed the total number of products published on my site, and now I want the code to display the total number of successful purchases from my site to the user.
The code I wrote to display the number of products on my site is as follows:
function product_count_shortcode() {
$count_posts = wp_count_posts( 'product' );
return $count_posts->publish;
}
add_shortcode( 'product_counthalsho', 'product_count_shortcode' );
You will basically have to show the number of orders that have the status completed. Just create a function that will query the order post_type and with the status completed.
You can simply use get_posts and use the php function count the check how many results you have, or use WP_Query that already has a property in the return object that tells you how many orders you have with that status.
Later Edit:
function mrc_total_completed_orders() {
$args = array(
'post_type' => 'order',
'posts_per_page' => -1,
'post_status' => 'completed',
);
$orders = get_posts( $args );
return count( $orders ); }
Or you can use the WC_Order_Query
function mrc_mrc_total_completed_Orders() {
$query = new WC_Order_Query( array(
'limit' => 99999,
'status' => array( 'completed' ),
'return' => 'ids',
) );
$orders = $query->get_orders();
return count( $orders ); }
Also some documentation here
Note that I haven't tested these solutions, but its enough to get you started :)
Usage:
echo mrc_mrc_total_completed_Orders();

Why wordpress orderby post__in is not sorting properly?

Wordpress query is sometime a pain, i'm trying to order posts by post__in like this :
$query = array(
"post_type" => "product",
"post_status" => "publish",
"posts_per_page" => 12,
"paged" => $paged,
"post__in" => $posts_ids, // <= array of ids (int)
"orderby" => "post__in"
);
remove_all_filters('pre_get_posts');
remove_all_filters('posts_orderby');
// Start query
$posts = new Wp_Query($query);
But it still output bad order, i have wp version 5.5.3 and from [doc][1] orderby by post__in is available from 3.5, i have already tried ("orderby" => array("post__in")) without success too, i have no third party plugin which may alter this parameter.
Does a tax_query may cause this issue?
Someone can help me please? i missed something ?
[1]: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
Add wp_reset_query() before the custom query.
$query = array(
"post_type" => "product",
"post_status" => "publish",
"posts_per_page" => 12,
"paged" => $paged,
"post__in" => $posts_ids, // <= array of ids (int)
"orderby" => "post__in"
);
wp_reset_query();
// Start query
$posts = new Wp_Query($query);

How to get quantity of a WooCommerce subscription?

I am currently developing a WordPress project and I am using WooCommerce with WooCommerce Subscriptions plugin to offer subscriptions to my users. I need help on how to get the quantity of a subscription in PHP.
I am using this code to get subscription but I can not retrieve quantity:
$subscriptions = wcs_get_subscriptions( array(
'customer_id' => get_current_user_id(),
'subscription_status' => 'wc-active',
'order_by' => 'DESC',
'subscriptions_per_page' => - 1
) );
When a user purchases a subscription, the user can select the quantity of subscriptions. So I need to get the value of this field:
Your code is correct and wcs_get_subscriptions() is the right and best way to get customer active subscriptions.
But you have missed a something after your code to get the customer subscription item quantity (code commented):
// Get current customer active subscriptions
$subscriptions = wcs_get_subscriptions( array(
'customer_id' => get_current_user_id(),
'subscription_status' => 'wc-active',
'order_by' => 'DESC',
'subscriptions_per_page' => - 1
) );
if ( count( $subscriptions ) > 0 ) {
// Loop through customer subscriptions
foreach ( $subscriptions as $subscription ) {
// Get the initial WC_Order object instance from the subscription
$order = wc_get_order( $subscription->get_parent_id() );
// Loop through order items
foreach ( $order->get_items() as $item ) {
$product = $item->get_product(); // Get the product object instance
// Target only subscriptions products type
if( in_array( $product->get_type(), ['subscription', 'subscription_variation'] ) ) {
$quantity = $item->get_quantity(); // Get the quantity
echo '<p>Quantity: ' . $quantity . '</p>';
}
}
}
}
Tested and works.
Here is my working code try this
$current_user_id = get_current_user_id();
$customer_subscriptions = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(), // Or $user_id
'post_type' => 'shop_subscription', // WC orders post type
'post_status' => 'wc-active' // Only orders with status "completed"
) );
And if you want get all post_status subscription then use this
$customer_subscriptions_for_other_cases = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(), // Or $user_id
'post_type' => 'shop_subscription', // WC orders post type
'post_status' => array('wc-on-hold','wc-pending-cancel','wc-active') // Only orders with status "completed"
) );
Thanks

Get processing status orders count in WooCommerce?

I want to get the processing order count in WooCommerce. I'm using the following code in the Code Snippet plugin but that is working.
if( !function_exists( 'wc_processing_order_count' ) ) {
require_once '../plugins/woocommerce/includes/wc-order-functions.php';
}
// NOTICE! Understand what this does before running.
$result = wc_processing_order_count();
It's returning nothing.
This custom function use a very light SQL query to get the orders count from a specific status:
function get_orders_count_from_status( $status ){
global $wpdb;
// We add 'wc-' prefix when is missing from order staus
$status = 'wc-' . str_replace('wc-', '', $status);
return $wpdb->get_var("
SELECT count(ID) FROM {$wpdb->prefix}posts WHERE post_status LIKE '$status' AND `post_type` LIKE 'shop_order'
");
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Usage example for "processing" orders count:
// Display "processing" orders count
echo get_orders_count_from_status( "processing" );
Both the answers above are way too complicated for something so simple.
Simplest way is this :)
$processing_orders_count = count(wc_get_orders( array(
'status' => 'processing',
'return' => 'ids',
'limit' => -1,
)));
This method could help you. The order is stored as post_type shop_order. So by creating query to get all posts of type shop_order and by passing arguments to get all processing order, you will be able to get those orders
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('processing')
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ){
$loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
}
Take a look at: https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
That reference really helped me in a similar situation.
Using the WC functions ensures your code is more future proof compared to using a hardcoded query or WP_Query as mentioned in other answers.

Filter orders using a WP_Query from an array of orders IDs in WooCommerce

I got an array of orders ID's and 'post__in' => $orders_ids to embed them in a WP_Query:
$filters_orders = array(
'post_status' => 'processing',
'post_type' => 'shop_order',
'posts_per_page' => 10,
'post__in' => $orders_ids,
'orderby' => 'modified',
'order' => 'ASC'
);
$loop = new WP_Query($filters_orders);
while ($loop->have_posts()) {
$loop->the_post();
$order = new WC_Order($loop->post->ID);
<HERE_MY_CUSTOM_HTML_TABLE>
}
I am filtering "processing" order status only, but it doesn't work and I get all kind of status.
What I am doing wrong? How to filter order status correctly in this WP_Query?
In a WP_Query, you need to use the post_status slugs like in the database table wp_posts… All orders status start with "wc-":
So in your case: 'post_status' => 'wc-processing'.
This should work now.

Categories