Use this as an AutomateWoo Custom Function action on a subscription workflow. It moves the next payment three months forward and adds an order note. WooCommerce Subscriptions is required.
Developer note:
Add custom PHP in a small custom plugin or child theme, and test it on a staging site before production. This example requires ongoing maintenance when extension APIs change.
functions.php
<?php
/**
* Extend a subscription's next payment by three months.
*
* @param AutomateWoo\Workflow $workflow Current workflow.
*/
function my_store_extend_subscription_renewal( $workflow ) {
$subscription = $workflow->data_layer()->get_subscription();
if ( ! $subscription instanceof WC_Subscription ) {
return;
}
$old_date = $subscription->get_date( 'next_payment' );
$old_time = $old_date ? wcs_date_to_time( $old_date ) : 0;
if ( ! $old_time ) {
return;
}
$new_time = wcs_add_time( 3, 'month', $old_time );
$new_date = gmdate( 'Y-m-d H:i:s', $new_time );
$subscription->update_dates( array( 'next_payment' => $new_date ) );
$subscription->add_order_note(
sprintf(
/* translators: 1: old date, 2: new date, 3: workflow title. */
__( 'Next payment moved from %1$s to %2$s by workflow “%3$s”.', 'my-store' ), // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
wp_date( wc_date_format(), $old_time ),
wp_date( wc_date_format(), $new_time ),
$workflow->get_title()
)
);
}