WooCommerce get order total

WooCommerce get order total

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging WooCommerce Order Totals in Custom Checkout Scripts

Attempting to retrieve an order total for a Google AdWords conversion inside a custom checkout handler often runs into scope issues rather than API limitations. The specific error you are encountering—Call to a member function get_formatted_order_total() on a non-object—indicates that the $order variable does not exist in the current execution context. This is a classic symptom of trying to access global state inside an environment where it hasn't been initialized or has gone out of scope.

In WooCommerce, the $order object is typically passed as the first argument to your hook callbacks (like woocommerce_thankyou) or exists within the local scope of specific template files. If you are placing this code directly in a file like thankyou.php outside of a function, or if you are attempting to access it from a separate AJAX endpoint without proper data passing, the variable will simply be undefined.

The regex approach used to strip non-numeric characters is also a fragility point. The method $order->get_formatted_order_total() returns a string formatted for display, often including currency symbols (like £) and spaces. While preg_replace('#[^\d.]#', '', ...) works to extract the number, relying on string manipulation for financial data is generally discouraged in modern PHP development due to potential encoding issues or unexpected formatting changes in WooCommerce versions.

Accessing the Order Object Correctly

To resolve the "non-object" error, you must ensure the $order variable is explicitly resolved before use. If you are working within a standard WooCommerce hook like woocommerce_thankyou, the global $woocommerce object provides access to the current session data.

Here is a more robust pattern for retrieving the total inside a function:

function get_order_total_for_conversion() {
    // Ensure we are in the checkout context and an order exists
    if ( ! wc_get_order_id() ) {
        return false;
    }

    $order = wc_get_order( wc_get_order_id() );

    if ( ! $order ) {
        return false;
    }

    // Get the total as a float directly, avoiding string formatting issues
    $total = $order->get_total();

    return round( floatval( $total ), 2 );
}

Using wc_get_order() is safer than assuming $order is globally available. It fetches the instance based on the session cart ID or order ID stored in the WooCommerce context. This decouples your code from the specific file location, making it reusable across different hooks like woocommerce_thankyou, wp_footer, or custom AJAX handlers.

Integrating with Google AdWords Conversion

Once you have a reliable $total value, passing it to the conversion script requires careful concatenation to avoid breaking the JavaScript syntax. The original code mixes PHP and JavaScript in a way that is difficult to maintain and debug.

A cleaner approach separates the data retrieval from the output generation:

function render_ga_conversion_script() {
    $order_total = get_order_total_for_conversion();

    if ( ! $order_total ) {
        return;
    }

    ?>
    <script type="text/javascript">
        /* <![CDATA[ */
        var google_conversion_id = 1066553725;
        var google_conversion_language = "en";
        var google_conversion_format = "3";
        var google_conversion_color = "ffffff";
        var google_conversion_label = "CzWXCLmwn1YQ_aLJ_AM";
        var google_conversion_value = <?php echo $order_total; ?>;
        var google_conversion_currency = "GBP";
        var google_remarketing_only = false;
        /* ]]> */
    </script>
    <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"></script>
    <noscript>
        <div style="display:inline;">
            <img height="1" width="1" style="border-style:none;" alt="" 
                 src="//www.googleadservices.com/pagead/conversion/1066553725/?value=<?php echo $order_total; ?>&amp;currency_code=GBP&amp;label=CzWXCLmwn1YQ_aLJ_AM&amp;guid=ON&amp;script=0"/>
        </div>
    </noscript>
    <?php
}

This structure ensures that if the order object is missing, no script tags are rendered, preventing potential layout shifts or JavaScript errors on the page.

Best Practices for Financial Data

When dealing with monetary values in PHP, always use floatval() or double types, but be aware of floating-point precision issues. For database storage or strict financial calculations, consider using the BC Math functions or rounding to two decimal places immediately after retrieval. However, for passing data to a third-party API like Google AdWords via JavaScript variables, standard floats are usually acceptable.

If you need to persist this logic across different environments or plugins, wrapping the retrieval in a function is superior to inline code. This pattern allows you to test the order fetching logic independently without worrying about the surrounding file context.

For more advanced e-commerce integrations, including how to handle refunds or partial payments dynamically, you might find additional resources at https://laravelcompany.com, though the core WooCommerce API methods remain consistent regardless of the external resource.

Our network