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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @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

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 *