phpwordpresswoocommercehook-woocommercewoocommerce-subscriptions

How to Change Payment Retry Rules for Woocommerce Subscriptions?


I am looking to change the payment retry rules.

I’m following the guide on https://woocommerce.com/document/subscriptions/develop/failed-payment-retry/

I’ve found the file wp-content/plugins/woocommerce-subscriptions/includes/payment-retry/class-wcs-retry-rules.php which seems to be where they are specified.

How do I override the rules so they’re not reset every time I update the plugin? I am looking to retry after 24 hours and 7 days e.g.

    $this->default_retry_rules = apply_filters( 'wcs_default_retry_rules', array(
        array(
            'retry_after_interval' => DAY_IN_SECONDS, // 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 * 7,
            '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',
        ),
    ) );
}

Solution

  • You should never overwrite any plugin core files, for many different reasons.

    In your case, wcs_default_retry_rules is the right filter hook to change WooCommerce Subscriptions Payment Retry rules.

    So the correct code to be used instead is:

    add_filter( 'wcs_default_retry_rules', 'custom_wcs_default_retry_rules' );
    function custom_wcs_default_retry_rules( $retry_rules ) {
        return array(
            array(
                'retry_after_interval' => DAY_IN_SECONDS, // 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 * 7,
                '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',
            ),
        );
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.

    Don't forget to replace the original plugin file for class-wcs-retry-rules.php file.