Summoning builders…Compiling dreams…Polishing pixels…Aligning the stars to your roadmap…Brewing the backlog…Forging gold from ideas…Charting the constellations…Waking the render spirits…

    Insights/Web3 & Emerging Tech/Retrieving Orders From WordPress Woocommerce

    Retrieving Orders From WordPress Woocommerce

    admin
    November 05, 2019
    6 views
    Web3 & Emerging Tech
    tutorials
    woocommerce
    wordpress
    e-commerce
    Retrieving Orders From WordPress Woocommerce

    An order is more than a line in your sales history. It tells you what a customer bought, whether payment arrived, where the goods should go, and what your team needs to do next. Being able to retrieve the right orders quickly is essential for customer support, fulfillment, reporting, and integrations.

    WooCommerce gives you several ways to do that. Store staff can find orders in the WordPress dashboard, developers can retrieve them through WooCommerce’s PHP functions, and external applications can use its REST API. The best approach depends on where you need the information and what you intend to do with it.

    This guide reflects WooCommerce workflows available in 2019; tools and interfaces may have changed since then.

    Choose the right way to retrieve orders

    Before writing code, decide which of these routes fits your task:

    • WordPress dashboard: Best for checking an individual purchase, answering a customer question, or reviewing orders awaiting action.
    • WooCommerce PHP API: Best for a custom plugin, an internal report, or another feature running inside WordPress.
    • WooCommerce REST API: Best for connecting an external application, warehouse system, or reporting service.

    All three routes retrieve orders, but they have different access requirements. Dashboard users need an appropriate role, PHP code needs WordPress and WooCommerce loaded, and REST clients need authentication credentials.

    Retrieve orders from the WordPress dashboard

    For everyday store operations, start here. You do not need an API key or custom code.

    1. Sign in with the appropriate account

    Open your WordPress login page, commonly located at /wp-admin/, and sign in. An administrator or WooCommerce shop manager normally has access to order management.

    If you cannot see WooCommerce in the menu, check your account permissions and confirm that the plugin is active. Avoid granting full administrator access merely to let someone process orders; use the shop manager role where its permissions are appropriate.

    2. Open the order list

    In the left-hand menu, select WooCommerce → Orders. This screen lists customer orders and provides summary information such as the order number, customer, status, date, and total.

    The exact columns can vary by WooCommerce version and installed extensions. If an expected column is missing, check Screen Options near the top of the page.

    3. Filter and search for the order

    Use the status links to narrow the list to orders such as Processing, On hold, or Completed. Date filters help when you know roughly when a purchase occurred.

    The order search can help locate a purchase using an order ID or customer information, such as a billing name or email address. Search behavior may differ when extensions customize order numbers or searchable fields.

    For a support request, ask for the order number and billing email first. These are usually more useful than a customer’s name alone, particularly when several customers share a name.

    4. Inspect the order details

    Click the order number or identifying link to open the order editor. Review:

    • Products, quantities, and line totals.
    • Billing and shipping information.
    • Shipping charges, taxes, discounts, and refunds.
    • Payment method and current order status.
    • Order notes, including status changes and gateway messages.

    Order notes are especially useful when a payment or fulfillment event needs explanation. Check them before assuming that an order’s current status tells the whole story.

    5. Interpret the status before taking action

    A retrieved order is not automatically a paid order. Common statuses include:

    • Pending payment: The order has been received, but payment has not yet been confirmed.
    • On hold: The order is awaiting confirmation or another manual step, often related to payment.
    • Processing: Payment has generally been received, and the order awaits fulfillment.
    • Completed: The order has been marked fulfilled.
    • Cancelled, Refunded, or Failed: The order needs a different workflow from an ordinary fulfillment request.

    Payment gateways and extensions can influence status changes. For a disputed or unclear payment, compare the order notes with the payment provider’s records rather than relying only on the status label.

    Retrieve orders inside WordPress with PHP

    For code running in WordPress, use WooCommerce’s order functions rather than querying database tables directly. In the 2019 data model, orders are backed by WordPress posts and metadata, but your application should not depend unnecessarily on that storage structure.

    The primary tools are wc_get_orders() for a collection and wc_get_order() for a single order.

    1. Query a limited set of orders

    Place this logic in a custom plugin or another controlled WordPress execution path that runs after WooCommerce has loaded:

    if ( ! function_exists( 'wc_get_orders' ) ) {
        return;
    }
    
    $orders = wc_get_orders( array(
        'status'  => array( 'wc-processing', 'wc-on-hold' ),
        'limit'   => 20,
        'orderby' => 'date',
        'order'   => 'DESC',
    ) );
    
    foreach ( $orders as $order ) {
        printf(
            '<p>Order #%s, %s %s</p>',
            esc_html( $order->get_order_number() ),
            esc_html( $order->get_total() ),
            esc_html( $order->get_currency() )
        );
    }
    

    This returns up to 20 matching order objects, newest first. The PHP query uses WooCommerce’s prefixed status names, such as wc-processing.

    Escaping output matters even in internal tools. Also restrict any page displaying order data to authorized users; running inside WordPress does not automatically make a custom page private.

    2. Retrieve one order and its items

    When you know the internal order ID, request the order directly:

    $order_id = 123;
    $order    = wc_get_order( $order_id );
    
    if ( ! $order ) {
        return;
    }
    
    $status        = $order->get_status();
    $billing_email = $order->get_billing_email();
    $total         = $order->get_total();
    
    foreach ( $order->get_items() as $item ) {
        $product_name = $item->get_name();
        $quantity     = $item->get_quantity();
        $line_total   = $item->get_total();
    
        // Use these values in your authorized internal workflow.
    }
    

    The internal ID is not necessarily the same as the displayed order number. Extensions that provide sequential or customized numbers can make them differ. Use the internal ID for programmatic lookups and the displayed number for customer-facing references.

    An item’s line total is not the complete order total: shipping, taxes, discounts, and other lines affect the final amount. Use the appropriate order getters instead of rebuilding totals from product prices.

    3. Paginate larger queries

    Avoid loading an entire order history into memory. For reports or exports, request manageable pages:

    $result = wc_get_orders( array(
        'limit'        => 50,
        'page'         => 1,
        'paginate'     => true,
        'date_created' => '2019-01-01...2019-01-31',
        'orderby'      => 'ID',
        'order'        => 'ASC',
    ) );
    
    $orders      = $result->orders;
    $total       = $result->total;
    $total_pages = $result->max_num_pages;
    

    With paginate enabled, the result contains the orders and pagination information. Increment page until you have processed the available pages. Check timezone assumptions whenever date boundaries matter, especially for financial reports.

    Retrieve orders through the REST API

    The REST API lets an application outside WordPress request order data over HTTP. For a compatible 2019 installation, WooCommerce 3.5 or later provides the wc/v3 API namespace.

    1. Prepare the store and credentials

    Confirm that the store uses HTTPS. Under Settings → Permalinks, choose a setting other than Plain if necessary for REST API routing.

    Then go to WooCommerce → Settings → Advanced → REST API and create a key:

    1. Enter a description identifying the integration.
    2. Select a user with appropriate order-access permissions.
    3. Choose Read permissions if the integration only retrieves data.
    4. Generate and securely save the consumer key and consumer secret.

    Keys inherit restrictions from their associated user as well as their configured access level. Treat the secret like a password: keep it out of source control, browser JavaScript, screenshots, and shared logs.

    2. Request a page of orders

    Over HTTPS, use HTTP Basic authentication with the consumer key as the username and consumer secret as the password. This command assumes those credentials are available as server-side environment variables:

    curl --user "${WC_CONSUMER_KEY}:${WC_CONSUMER_SECRET}" \
      --get "https://example.com/wp-json/wc/v3/orders" \
      --data-urlencode "status=processing" \
      --data-urlencode "per_page=20" \
      --data-urlencode "page=1"
    

    Replace example.com with the store’s domain, including any WordPress installation subdirectory. Unlike the PHP example, this REST request uses processing, without the wc- prefix.

    The response is a JSON array of orders. Each order includes fields such as id, status, currency, total, billing, shipping, and line_items. Monetary amounts commonly arrive as strings; avoid careless floating-point arithmetic when processing them.

    3. Retrieve one order

    Append the internal order ID to the endpoint:

    curl --user "${WC_CONSUMER_KEY}:${WC_CONSUMER_SECRET}" \
      "https://example.com/wp-json/wc/v3/orders/123"
    

    A successful request returns one order object rather than an array. Check the HTTP status code before parsing a response as a successful order retrieval. Error responses have a different structure.

    4. Follow pagination deliberately

    The collection endpoint is paginated. The per_page parameter supports up to 100 items per request, and response headers include X-WP-Total and X-WP-TotalPages.

    Use curl -i during testing to inspect headers. In application code, read those headers and request subsequent pages rather than assuming the first response contains every order.

    For recurring imports, store the WooCommerce order ID in the destination system and update existing records instead of inserting duplicates. Creation-date filters help with historical imports, but they do not detect every later change to older orders. Use an appropriate reconciliation process or order-update webhooks when ongoing synchronization is required.

    Common pitfalls and troubleshooting

    An empty result is not necessarily an error. Check status filters, date ranges, customer filters, and whether you are querying the correct store or staging environment.

    Authentication failures need more than a new key. Check credentials, key permissions, and the associated user’s capabilities. If correct credentials still fail, verify that the web server or proxy forwards the Authorization header to PHP.

    A missing endpoint may be a routing problem. Confirm the WooCommerce version, API namespace, installation URL, and permalink configuration before changing your application code.

    Large requests can exhaust resources. Paginate, process batches, and avoid retrieving full order objects when your PHP workflow only needs IDs; wc_get_orders() supports 'return' => 'ids'.

    Order data is private customer data. Do not expose API credentials or order responses through public pages. Limit saved fields to what the integration needs, restrict access, and avoid logging complete billing or shipping records.

    Takeaway

    Use the dashboard for everyday order checks, WooCommerce’s PHP functions for work inside WordPress, and the REST API for external applications. Whichever route you choose, retrieve only what you need, verify status and identity carefully, paginate larger collections, and protect the customer information that comes with every order.

    Conversation (0)

    No comments yet. Start the conversation.

    Join the conversation

    Create a free member account to comment.