Skip to content

Instantly share code, notes, and snippets.

@thenbrent
Last active September 8, 2020 14:02
Show Gist options
  • Save thenbrent/34656a3c97ed2003166b to your computer and use it in GitHub Desktop.
Save thenbrent/34656a3c97ed2003166b to your computer and use it in GitHub Desktop.
WooCommerce Subscriptions Failed Payment Retry Rules for Testing: Replace the default WooCommerce Subscriptions failed payment retry rules with rules that run every few minutes (not days) and always send dunning emails to help with testing.
<?php
/**
* Plugin Name: WooCommerce Subscriptions Failed Payment Retry Rules for Testing
* Plugin URI:
* Description: Replace the default WooCommerce Subscriptions failed payment retry rules with rules that run every few minutes (not days) and always send dunning emails to help with testing.
* Author: Prospress Inc.
* Author URI: http://prospress.com/
* Version: 1.0
*
* Copyright 2016 Prospress, Inc. (email : [email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package WooCommerce Subscriptions
* @author Prospress Inc.
* @since 1.0
*/
/**
* Apply our own set of retry rules.
*/
function wcsfptrr_retry_rules( $default_retry_rules_array ) {
return array(
array(
'retry_after_interval' => 60,
'email_template_customer' => 'WCS_Email_Customer_Payment_Retry',
'email_template_admin' => 'WCS_Email_Payment_Retry',
'status_to_apply_to_order' => 'pending',
'status_to_apply_to_subscription' => 'on-hold',
),
array(
'retry_after_interval' => 120,
'email_template_customer' => 'WCS_Email_Customer_Payment_Retry',
'email_template_admin' => 'WCS_Email_Payment_Retry',
'status_to_apply_to_order' => 'pending',
'status_to_apply_to_subscription' => 'on-hold',
),
array(
'retry_after_interval' => 180,
'email_template_customer' => 'WCS_Email_Customer_Payment_Retry',
'email_template_admin' => 'WCS_Email_Payment_Retry',
'status_to_apply_to_order' => 'pending',
'status_to_apply_to_subscription' => 'on-hold',
),
array(
'retry_after_interval' => 240,
'email_template_customer' => 'WCS_Email_Customer_Payment_Retry',
'email_template_admin' => 'WCS_Email_Payment_Retry',
'status_to_apply_to_order' => 'pending',
'status_to_apply_to_subscription' => 'on-hold',
),
array(
'retry_after_interval' => 300,
'email_template_customer' => 'WCS_Email_Customer_Payment_Retry',
'email_template_admin' => 'WCS_Email_Payment_Retry',
'status_to_apply_to_order' => 'pending',
'status_to_apply_to_subscription' => 'on-hold',
),
);
}
add_filter( 'wcs_default_retry_rules', 'wcsfptrr_retry_rules' );
@vvp242424
Copy link

Correct me if I'm wrong, but currently woocommerce doesn't do any re-running of failed subscriptions. is this just generating emails or actually retrying to charge?

@thenbrent
Copy link
Author

@vvp242424 these were written to help test the new Failed Recurring Payment Retry system that is being introduced with Subscriptions v2.1.

@frnkoc
Copy link

frnkoc commented Dec 14, 2016

Hi.
How do you apply this file? Adding this code in the functions.php or as a plugin or in the child theme?
Also in the developers guide https://docs.woocommerce.com/document/subscriptions/develop/failed-payment-retry/
shows the custom code " function eg_my_custom_retry_rules( $default_retry_class ) { "
the "$default_retry_class" has to be included? I dont see it in your code.
I'm trying to filter the default rules to remove the last 3 tries leaving only the first two at 12 hours no email and 24 hours sending email.
This is the way i tried but the payment was not being retried and no "Order Notes" were added to the order.

function pf_custom_retry_rules( $default_retry_class ) {
return array(
array(
'retry_after_interval' => DAY_IN_SECONDS / 2, // how long to wait before retrying
'email_template_customer' => '', // don't bother the customer yet
'email_template_admin' => 'WCS_Email_Payment_Retry',
'status_to_apply_to_order' => 'pending',
'status_to_apply_to_subscription' => 'on-hold',
),
array(
'retry_after_interval' => DAY_IN_SECONDS / 2,
'email_template_customer' => 'WCS_Email_Customer_Payment_Retry',
'email_template_admin' => 'WCS_Email_Payment_Retry',
'status_to_apply_to_order' => 'pending',
'status_to_apply_to_subscription' => 'on-hold',
),
);
}
add_filter( 'wcs_retry_rule_class', 'pf_custom_retry_rules' );

@james-allan
Copy link

@frnkoc the example in the doc you referenced is incorrect. The correct filter hook is wcs_default_retry_rules. We'll get that fixed up.

Just to clarify your code snippet should look like this:

function pf_custom_retry_rules( $default_retry_class ) {
	return array(
		array(
			'retry_after_interval' => DAY_IN_SECONDS / 2, // how long to wait before retrying
			'email_template_customer' => '', // don't bother the customer yet
			'email_template_admin' => 'WCS_Email_Payment_Retry',
			'status_to_apply_to_order' => 'pending',
			'status_to_apply_to_subscription' => 'on-hold',
		),
		array(
			'retry_after_interval' => DAY_IN_SECONDS / 2,
			'email_template_customer' => 'WCS_Email_Customer_Payment_Retry',
			'email_template_admin' => 'WCS_Email_Payment_Retry',
			'status_to_apply_to_order' => 'pending',
			'status_to_apply_to_subscription' => 'on-hold',
		),
	);
}
add_filter( 'wcs_default_retry_rules', 'pf_custom_retry_rules' );

the "$default_retry_class" has to be included? I dont see it in your code.

In your case you don't necessarily need to include the parameter here, you only need to include it if you want to change parts without needing to completely rewrite the entire array of data.

@neoseeyou
Copy link

Hello

Thank you for this code. I am using Stripe and i want to exclude SEPA from the retry process. Only apply the retry process to orders with Bank card?

Do you think a function in my functions.php can override native plugin behaviour?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment