Setup a Failed Order Notification for WooCommerce

There are numerous reasons why an order might fail. The most common reason is an error in the payment processor. This could be a one time thing or maybe a glitch with the payment processor. In any case, it would be good to follow up with the customer. It would be a shame if you didn’t do anything as the customer was already in the checkout/buying process and didn’t get to complete their order.

This can be one of the easiest ways to retrieve lost revenue.

Setting up a failed order notification in WooCommerce

WooCommerce automatically sets an order to failed when something goes wrong. With a simple code snippet you can send yourself an email or push notification, so you can follow up as soon as possible with the customer. In my experience that would be an amazing customer experience, right?

Setting up a failed order email notification

WooCommerce includes an email for failed orders. You can enable this email by going to WooCommerce > Settings > Emails, then click on Failed order

If this email does not suit your needs you can use the following code snippet and our instructions on adding a code snippet to your site to send an email, or

/**
 * Add a failed order email notification
 */
function sp_failed_order_email_notification( $order_id ) {

	$order     = wc_get_order( $order_id );

	/**
	 * Do something custom here when an order fails.
	 */

	/**
	 * Send an email
	 */
	$to        = get_option( 'admin_email' );
	$subject   = 'A order failed!';
	$message   = sprintf( __( '%1$s went to the `failed` order status. This may require action to follow up.', 'text-domain' ), '<a class="link" href="' . admin_url( 'post.php?post=' . $order_id . '&action=edit' ). '">' . sprintf( __( 'Order #%s', 'woocommerce'), $order->get_order_number() ) . '</a>' );
	$headers[] = 'From: WooCommerce Store <[email protected]>';
	$headers[] = 'Cc: John Snow <[email protected]>'; // Add other email addresses here.
	$headers[] = 'Content-Type: text/html;';
	$headers[] = 'charset=UTF-8';

	wp_mail( $to, $subject, $message, $headers );

}
add_action( 'woocommerce_order_status_failed', 'sp_failed_order_email_notification', 10 );

Setting up a failed order push notification to your phone

Setting up a push notification is a bit more difficult than setting up an email notification. It requires your site be integrated with a service to do the push notification and thus requires a bit more setup. In this example, I will show you how you can send a push notification in combination with Pushover. If you haven’t set up an account yet, you can register at their site. Registration is free and so is the app you need to install on your smartphone. After the 7 days free trial, they require you to buy the smartphone app for a lifetime usage for up to 7500 messages per month (if you go above that you can get a monthly plan).

When you’ve set up your account, you will need two things from that account:
1) Your user key
2) Application key

The user key can be found directly in your dashboard when you go to Pushover.com and are logged in. To get an application key, you’d first need to register a new application. You can do this also from the initial dashboard.

pushover-register-application

When you’re registering a new application, you need to fill out the fields as seen in the screenshot above. As type you can enter ‘website’, descriptionurl, and icon can be any values you’d like, they’re optional values.

Setting up the notification

When you’ve retrieved the user and application keys, you can use the code snippet below. Copy/paste the contents to your (child) theme’s functions.php and replace the {user_key} and {application_key} to your keys.

/**
 * Add a failed order push notification
 */
function sp_failed_order_push_notification( $order_id ) {

	$request = array(
		'token'   => '{APPLICATION_KEY}', // Application token, found when registering a application in Pushover
		'user'    => '{USER_KEY}', // User token, found directly in your Pushover dashboard
		'title'   => 'Failed order',
		'message' => __( 'A order has failed, go get that cheddar!', 'textdomain' ),
		'url'     => admin_url( 'post.php?post=' . $order_id . '&action=edit' ),
	);

	$response = wp_remote_post(
		'https://api.pushover.net/1/messages.json',
		array(
			'body' => $request,
		)
	);

}

add_action( 'woocommerce_order_status_failed', 'sp_failed_order_push_notification' );

This code snippet will send the push notification as soon as an order goes to the failed orders status. It will send a link to the related order within the notification so you can view the order immediately.

Setting up the app

Make sure to download the Pushover notifications app from the app store on your iOS or Android smartphone and log in with your account. Pushover will recognize and register your phone in the website’s dashboard. You can send a test notification from there to see if everything is working fine.

That’s it for setting up a push notification when an order goes to the failed order status.

Wrapping up

Now that you’re getting notifications when an order fails, it’s easy to follow up with the customer. As mentioned earlier, this could be the easiest way of retrieving lost revenue since the customers were already late in the buying process when something went wrong.

Having someone reach out to you and ask how they can help you further can be a great customer experience.

Happy selling!

Keep up-to-date with the weekly newsletter

You will receive a bonus tips, actionable insight, and plugin updates straight to your inbox!

100% privacy guaranteed, unsubscribe with one click. Powered by ConvertKit

Get our best WooCommerce advice!

Delivered directly to your inbox

Your email is 100% private. We hate spam too. Powered by ConvertKit

Published by Shop Plugins