WooCommerce: Disable Tracking if Order Failed @ Thank You Page

The “woocommerce_thankyouhook fires on the Thank You page once an order is placed. Most tracking functions like Google Analytics, affiliate commission plugins and other WooCommerce extensions rely on “woocommerce_thankyou” to run their code.

Problem is – “woocommerce_thankyou” is ALSO called if an order fails (i.e. payment did not go through). Now, unless the plugin is smart enough in its own functions to exclude failed orders, which doesn’t happen often I’m afraid, we need to find a way NOT to run “woocommerce_thankyou” if an order fails. Case study: a client uses a third party affiliate plugin, this plugin hooks into “woocommerce_thankyou“, but they don’t want to calculate conversions when an order fails.

So here you go!

The default Thank You page in WooCommerce

PHP Snippet: Disable “woocommerce_thankyou” Action if WooCommerce Order = Failed

/**
 * @snippet       Remove "woocommerce_thankyou" Action if Order Fails - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.9
 * @community     https://businessbloomer.com/club/
 */
 
add_action( 'wp_head', 'bbloomer_tracking_exclude_failed_orders' );
 
function bbloomer_tracking_exclude_failed_orders() {
   global $wp;
   // ONLY RUN ON THANK YOU PAGE
   if ( ! is_wc_endpoint_url( 'order-received' ) ) return; 
   // GET ORDER ID FROM URL
   $order_id = absint( $wp->query_vars['order-received'] );
   $order = wc_get_order( $order_id );
   if ( $order->has_status( 'failed' ) ) {
      // DISABLE ANY FUNCTION HOOKED TO "woocommerce_thankyou"
      remove_all_actions( 'woocommerce_thankyou' );
   }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]
  • WooCommerce: How To Make A Website GDPR Compliant? (12 Steps)
    Ok, we all know that the EU General Data Protection Regulation (GDPR) will come into force on the 25th May 2018. So the main question is: what changes do we need to make on our WooCommerce website to become compliant? And another important query might be: how does GDPR affect non-European WooCommerce websites? In this […]
  • WooCommerce: How to Add a Custom Checkout Field
    Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping. Ideally, this custom field could show above the […]
  • WooCommerce: Add Content to the Thank You Page
    A client of mine wanted to add some text to the thank you page, the page that customers see after they place an order via the default WooCommerce Checkout page. In this case scenario, they wanted to add a special coupon discount in order to entice buyers to go back to the website and buy […]
  • WooCommerce: How to Add a Custom Order Status
    All WooCommerce orders go to either “processing“, “completed“, “on-hold” and other default order statuses based on the payment method and product type. Sometimes these statuses are not enough. For example, you might need to mark certain orders in a different way for tracking, filtering, exporting purposes. Or you might want to disable default emails by […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Disable Tracking if Order Failed @ Thank You Page

  1. I tried this snippet, but after installing it no conversions were tracked at all.

    1. Hi Antero, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

      1. Since this action is added to wp_head, the callback shouldn’t have any arguments defined. Currently you have $order_id defined as argument :

        function bbloomer_tracking_exclude_failed_orders( $order_id )

        But wp_head actions don’t receive any args. So it should be :

        function bbloomer_tracking_exclude_failed_orders()

        1. Yep, spot on! Snippet updated. Thank you!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *