wc_get_customer_order_count returns 0
-
Hi,
I have a strange case where, on the front end :
For one of my client, wc_get_customer_order_count($myclient_id) returns “0” when he has 3 orders. I tested for another recently registered client, he gets the same results : 0 (even though he ordered).I’m using this function to just display a count of orders on the front-end. In woocommerce admin, when I go to “clients”, the order count is correct.
For my other clients (registered months ago), everything works, the function returns the correct number of orders.
Now why my new clients are getting “zero” with this function ?
I’m using Woocommerce 5.5.1 and WordPress 5.8.
Regards
AB
-
Hi @abitp,
Testing the
wc_get_customer_order_counton my installations I was able to apply a User ID and fetch the correct number of orders for both existing and new users I created and tested with.When testing I used this code to output the number of orders a customer has on their account to the shop page using the
woocommerce_before_main_contenthook.function get_order_count_test() { // Pass different User IDs manually for testing output. $user_id = 36; $result = wc_get_customer_order_count( $user_id ); print_r( '<p>Testing Customer Order Counts: ' . $result . '</p>' ); } add_action( 'woocommerce_before_main_content', 'get_order_count_test' );Image Link: https://cloudup.com/c_Gkr-33kOq
The usage of the
wc_get_customer_order_countcan be found here.Hi,
Thank you, I’ve been exactly using it as the tutorial / Hookr instructions tell.
As I say, it’s working for most of my users.
But somehow, for recently registered users, it’s -> Not working. It’s returning “0” as order counts, even if this recently registered user has already ordered more than once.Also my code is as follows :
global $my_user_id;
$dejacommande = wc_get_customer_order_count( $my_user_id);I also tried using :
$args = [ 'author' => $my_user_id, 'post_status' => 'any', 'post_type' => 'shop_order' ];$query = new WP_Query($args);
$totalcount = $query->found_posts;And none work for recently registered users. It works for other users.
Hi there,
Here is an old mini plugin of mine, maybe that works better for you: https://a.cl.ly/GGupBAv4
Please note that we provide no warranty or support for custom code or 3rd party plugins (meaning we cannot not help you further with this code snippet or WordPress.org / GitHub plugins). Use them at your own risk.
**Also: Before you do that, please do make a full backup!** π
Kind regards,
Hi,
Thank you for your message.
I would say it’s not helping for my situation here.I just want the order count for the current user on the front end, just like it’s displayed correctly in the wp admin.
My problem is that on the front end the system considers that this user has 0 order, when asked using wc_get_customer_order_count. However, still on the front end, when this client goes to “my-orders” his orders are actually displayed correctly. Same for when I go to wp admin.
How is that possible ?
How to fix it ?
Thanks
Hi @abitp!
I am sorry that you are still having trouble with this.
When you say the “front end” where exactly are you trying to output the count?
Additionally, please send us the exact code you are using now. You can add it to a gist and link it here.
Cheers!
Hi @rynald0s, thank you for your reply.
I am trying to output it on the “my account – edit account details”. I’m calling it front end because it’s not in wp-admin.I’m inserting the code here.
<?php /** * Edit address form * * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-edit-address.php. * * HOWEVER, on occasion WooCommerce will need to update template files and you * (the theme developer) will need to copy the new files to your theme to * maintain compatibility. We try to do this as little as possible, but it does * happen. When this occurs the version of the template file will be bumped and * the readme will list any important changes. * * @see https://docs.woocommerce.com/document/template-structure/ * @package WooCommerce\Templates * @version 3.6.0 */ defined( 'ABSPATH' ) || exit; $page_title = ( 'billing' === $load_address ) ? esc_html__( 'Billing address', 'woocommerce' ) : esc_html__( 'Shipping address', 'woocommerce' ); $slugok = sanitize_title($page_title); do_action( 'woocommerce_before_edit_account_address_form' ); ?> <?php if ( ! $load_address ) : ?> <?php wc_get_template( 'myaccount/my-address.php' ); ?> <?php else : ?> <form method="post"> <h3><?php echo apply_filters( 'woocommerce_my_account_edit_address_title', $page_title, $load_address ); ?></h3><?php // @codingStandardsIgnoreLine ?> <div class="woocommerce-address-fields <?php echo $slugok;?>"> <?php do_action( "woocommerce_before_edit_address_form_{$load_address}" ); ?> <div class="woocommerce-address-fields__field-wrapper"> <?php if($slugok == 'adresse-de-facturation') { global $my_user_id; $dejacommande = wc_get_customer_order_count( $my_user_id ); $disabled = ($dejacommande >= 1) ? 'disabled_' : ''; $readonly = ($dejacommande >= 1) ? ['readonly' => true] : ''; $args = [ 'author' => $my_user_id, 'post_status' => 'any', 'post_type' => 'shop_order' ]; $query = new WP_Query($args); $totalcount = $query->found_posts; echo '<div class="test_" style="display:none;">'.$totalcount.'</div>'; echo '<div class="test_" style="display:none;">'.$my_user_id.'</div>'; } foreach ( $address as $key => $field ) { if($slugok == 'adresse-de-facturation') { array_push($field['class'],$disabled); $field['custom_attributes'] = $readonly; } woocommerce_form_field( $key, $field, wc_get_post_data_by_key( $key, $field['value'] ) ); } ?> </div> <?php do_action( "woocommerce_after_edit_address_form_{$load_address}" ); ?> <p> <button type="submit" class="button" name="save_address" value="<?php esc_attr_e( 'Save address', 'woocommerce' ); ?>"><?php esc_html_e( 'Save address', 'woocommerce' ); ?></button> <?php wp_nonce_field( 'woocommerce-edit_address', 'woocommerce-edit-address-nonce' ); ?> <input type="hidden" name="action" value="edit_address" /> </p> </div> </form> <?php endif; ?> <?php do_action( 'woocommerce_after_edit_account_address_form' ); ?>The variables $totalcount and $dejacommande both return “0” for the clients that have this bug (when they actually have some orders).
Hi @abitp!
Try the following code in your themes’ functions.php file and not in the template:
add_action( 'woocommerce_before_account_navigation', 'rs_get_all_orders' ); function rs_get_all_orders() { if ( is_user_logged_in() ) { $user_id = get_current_user_id(); $customer = new WC_Customer( $user_id ); $last_order = $customer->get_last_order(); $order_id = $last_order->get_id(); $order_data = $last_order->get_data(); $order_status = $last_order->get_status(); $result = wc_get_customer_order_count( $user_id ); foreach ( $last_order->get_items() as $item ) { echo ( '<p>Total orders: ' . $result . '</p>' ); } } }Hi,
Thank you for your reply.
I slightly edited the code because I’m not using the woocommerce_account_navigation hook.
(code below)
For the customer I keep mentionning (who has actually 3 orders but on the front end it says zero) : I got this result : Total orders: 0.Not okay :/
(the code I slightly edited and put in my functions.php as asked)
add_action( 'woocommerce_order_details_before_order_table', 'rs_get_all_orders' ); function rs_get_all_orders() { if ( is_user_logged_in() ) { $user_id = get_current_user_id(); $customer = new WC_Customer( $user_id ); $last_order = $customer->get_last_order(); $order_id = $last_order->get_id(); $order_data = $last_order->get_data(); $order_status = $last_order->get_status(); $result = wc_get_customer_order_count( $user_id ); foreach ( $last_order->get_items() as $item ) { echo ( '<p style="display:none;" id="test">Total orders: ' . $result . '</p>' ); } } }Hi @abitp!
What status are these orders in for the customer?
Also, please use snipoard.io to send me a screenshot of what you are seeing and where exactly you are outputting.
Cheers!
Hi,
As I read your question about the order statuses,
I thought maybe the problem was because of these orders have one of my custom order status.I’m using this plugin : WooCommerce Order Status Manager https://woocommerce.com/fr-fr/products/woocommerce-order-status-manager/
As I changed the client order statuses to “completed”, the order count worked out and showed “3”.
Do you still need a screenshot ?
Also is this a woocommerce / plugin update situation ?
Cheers
Hi @abitp!
Nice! Nope, no longer need the screenshot.
You will then need to adapt the code to factor in all order statuses, and not only completed. Something along the lines of using
'post_status' => 'wc-completed'and adding the statuses to an array.Cheers!
Hi,
Here’s the code I wrote in my functions.php :if(is_user_logged_in()) { $my_user_id = get_current_user_id(); $customer = new WC_Customer( $my_user_id ); $statuses = array('processing','expedier','completed', 'refunded', 'pending', 'on-hold'); global $count_real; $count_real = $customer->get_order_count($statuses); }Where “expedier” is the custom order status I’ve mentioned.
Does it seem correct ?
Cheers
-
This reply was modified 4 years, 7 months ago by
ITP TECHNOLOGIE. Reason: replacing the code tag correctly
Hi again,
Does it seem correct ?
I don’t think so you dropped the foreach loop and
get_order_countrequires a customer object. For reference: https://woocommerce.github.io/code-reference/files/woocommerce-includes-interfaces-class-wc-customer-data-store-interface.html#source-view.32You could also try something like:
$orders = wc_get_orders(array( 'customer_id' => get_current_user_id(), 'return' => 'ids', )); echo count($orders);This gives you an array of order IDs which you can count and then output that count result.
Kind regards,
Hi,
`$orders = wc_get_orders(array(
‘customer_id’ => get_current_user_id(),
‘return’ => ‘ids’,
));
echo count($orders);`Returns “2” whatever the current user is.
Should I go back to the rs_get_all_orders function previously presented ?
Edit : I still can’t include my orders with the custom order statuses in my order count, even with the previously mentioned code.
Cheers
-
This reply was modified 4 years, 7 months ago by
ITP TECHNOLOGIE.
Hi again,
Returns β2β whatever the current user is.
That sounds like something is interfering in general. Does it work ok when you echo the user_id?
Should I go back to the rs_get_all_orders function previously presented ?
The previous tried functions should work and you mentioned that rs_get_all_orders got you no result either, so I am assuming this needs some in depth debugging.
Edit : I still canβt include my orders with the custom order statuses in my order count, even with the previously mentioned code.
Gotcha. Whatever is adding the orders might also need a custom integration / addition.
Kind regards,
-
This reply was modified 4 years, 7 months ago by
The topic ‘wc_get_customer_order_count returns 0’ is closed to new replies.