skip to Main Content

In my WooCommerce store, I have an input field where I want the user to enter the order number. When validating this field, I need to check if that order number actually exists in WooCommerce.

From this question, it seems like the WooCommerce order_number might not always be the same as the order_id (which is always the same as the post_id).

So I can’t use wc_get_order($the_order) function since the documentation states that it wants the post_id in the $the_order parameter.

I can’t find a specific function to get the order by the order number (neither in the documentation nor the code in github).

I’m fairly new to WooCommerce, so maybe I am missing something. Is there a way to do this at all? Or is the wc_get_order mis-documented?

I’ve done a lot of googling and searching here on stack overflow, but I really can’t find the answer to this! Any help is appreciated.

PS: I suppose I could get all orders and loop through them one by one checking if the number matches, but I was hoping to find a simpler solution to this 😀

3

Answers


  1. The reason you’re struggling to find anything about this in the documentation is because WooCommerce has no native concept of an order number. The ID is the order number.

    What store owners tend to find however is that order IDs don’t increase sequentially. There can be a large jump in ID between one order and the next because of the way WordPress handles posts. Often users will install a plugin to address that. It’s those plugins that introduce the differentiation between order IDs and what they refer to as the order number.

    Here’s an example of one such plugin: https://en-gb.wordpress.org/plugins/woocommerce-sequential-order-numbers/

    Included in the documentation is an example of how to find an order by its number. If you have one of these plugins installed, you’ll simply need to lookup how the plugin in question resolves numbers to IDs.

    Login or Signup to reply.
  2. You may try this if statement to see if given ID (number) is an Order Number or not:

    function is_it_a_shop_order($givenNumber)
    {   
        if(get_post_type($givenNumber) == "shop_order")
        {
            echo "yes this is a valid order number";
        }
        else
        {
            echo "no go away";
        }
    }
    

    WordPress stores all posts (pages, orders, products etc.) in wp_posts table and orders are stored with post type named “shop_order”.

    I tested the code. I hope this will help you. Have a good day.

    Login or Signup to reply.
  3. Send the order number through the wc_get_order call. If the returned array is empty, the order number does not exist.

    $bOrderExists = DoesOrderExist($OrderNumber);
    
    function DoesOrderExist($OrderNumber)
      {
      $orderq   = wc_get_order($OrderNumber);
      if(empty($orderq))
        return 0;
    
    return 1;
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search