Skip to content

Add Payment Link to WooCommerce Subscription Renewal Email

I’m currently working for a client that manages subscriptions with WordPress and WooCommerce (in particular using the WooCommerce Subscription plugin). Since most payment options they support are actually manual payments like bank transfer (BACS), we had to figure out a way to add a payment link to the WooCommerce subscription renewal email.

In order to accept manual renewals enable it in the WooCommerce Subscription settings (the documentation can be found here: WooCommerce Subscription Manual Renewal).

The template for the renewal email is called “Customer Renewal Invoice” and is usually only sent to the Customer.

Please keep in mind that if you’re working in a local or staging environment you need to specifically enable automatic payments (see: Enabling Automatic Payments for Staging Sites).

To add a payment link to the Customer Renewal Invoice we need to add a custom template in our theme directory.

Create a new file in /wp-content/themes/{your-theme}/woocommerce/emails/  and name it customer-renewal-invoice.php

Paste the following content into the file:

<?php
/**
 * Customer renewal invoice email
 *
 * @author  Brent Shepherd
 * @package WooCommerce_Subscriptions/Templates/Emails
 * @version 1.4.0
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
?>

<?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<?php if ( 'pending' == $order->get_status() ) : ?>
	<p>
		<?php
		// translators: %1$s: name of the blog, %2$s: link to checkout payment url, note: no full stop due to url at the end
		echo wp_kses( sprintf( _x( 'An invoice has been created for you to renew your subscription with %1$s. To pay for this invoice please use the following link: %2$s', 'In customer renewal invoice email', 'woocommerce-subscriptions' ), esc_html( get_bloginfo( 'name' ) ), '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now &raquo;', 'woocommerce-subscriptions' ) . '</a>' ), array( 'a' => array( 'href' => true ) ) );
		?>
	</p>
<?php elseif ( 'failed' == $order->get_status() ) : ?>
	<p>
		<?php
		// translators: %1$s: name of the blog, %2$s: link to checkout payment url, note: no full stop due to url at the end
		echo wp_kses( sprintf( _x( 'The automatic payment to renew your subscription with %1$s has failed. To reactivate the subscription, please login and pay for the renewal from your account page: %2$s', 'In customer renewal invoice email', 'woocommerce-subscriptions' ), esc_html( get_bloginfo( 'name' ) ), '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now &raquo;', 'woocommerce-subscriptions' ) . '</a>' ), array( 'a' => array( 'href' => true ) ) ); ?></p>
<?php endif; ?>

<?php do_action( 'woocommerce_subscriptions_email_order_details', $order, $sent_to_admin, $plain_text, $email ); ?>

<?php do_action( 'woocommerce_email_footer', $email ); ?>

At the position of the renewal email where you want to have the payment link add the following two lines of code:

<?php $pay_now_url = esc_url( $order->get_checkout_payment_url() ); ?>
<a href="<?php echo $pay_now_url; ?>"><?php echo __('Pay now'); ?></a>

Your final customer-renewal-invoice.php should look similar to this:

<?php
/**
 * Customer renewal invoice email
 *
 * @author  Brent Shepherd
 * @package WooCommerce_Subscriptions/Templates/Emails
 * @version 1.4.0
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
?>

<?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

<?php if ( 'pending' == $order->get_status() ) : ?>
	<p>
		<?php
		// translators: %1$s: name of the blog, %2$s: link to checkout payment url, note: no full stop due to url at the end
		echo wp_kses( sprintf( _x( 'An invoice has been created for you to renew your subscription with %1$s. To pay for this invoice please use the following link: %2$s', 'In customer renewal invoice email', 'woocommerce-subscriptions' ), esc_html( get_bloginfo( 'name' ) ), '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now &raquo;', 'woocommerce-subscriptions' ) . '</a>' ), array( 'a' => array( 'href' => true ) ) );
		?>
	</p>
<?php elseif ( 'failed' == $order->get_status() ) : ?>
	<p>
		<?php
		// translators: %1$s: name of the blog, %2$s: link to checkout payment url, note: no full stop due to url at the end
		echo wp_kses( sprintf( _x( 'The automatic payment to renew your subscription with %1$s has failed. To reactivate the subscription, please login and pay for the renewal from your account page: %2$s', 'In customer renewal invoice email', 'woocommerce-subscriptions' ), esc_html( get_bloginfo( 'name' ) ), '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Pay Now &raquo;', 'woocommerce-subscriptions' ) . '</a>' ), array( 'a' => array( 'href' => true ) ) ); ?></p>
<?php endif; ?>

<?php do_action( 'woocommerce_subscriptions_email_order_details', $order, $sent_to_admin, $plain_text, $email ); ?>

// Adds a payment link to enable customers to directly pay the invoice
<?php $pay_now_url = esc_url( $order->get_checkout_payment_url() ); ?>
<a href="<?php echo $pay_now_url; ?>"><?php echo __('Pay Now!'); ?></a>

<?php do_action( 'woocommerce_email_footer', $email ); ?>

And this is how you add a payment link to the WooCommerce subscription renewal email.

Happy coding!

Published inProgrammingWooCommerce

One Comment

  1. Paul

    Thank you, the snippet for the payment link that I found here is very useful!

Leave a Reply

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