Skip to content

Instantly share code, notes, and snippets.

@danielbitzer
Last active May 18, 2023 15:31
Show Gist options
  • Save danielbitzer/23efd86fac499c52f9bef3f07e32c41a to your computer and use it in GitHub Desktop.
Save danielbitzer/23efd86fac499c52f9bef3f07e32c41a to your computer and use it in GitHub Desktop.
AutomateWoo - Action function to change subscription next payment date
<?php
/**
* AutomateWoo action function to extend a subscription by 3 months.
*
* A note will be added to the subscription.
*
* Ensures that the dates near the end of the month are logically handled and do not shift to the start of the following month.
*
* Custom action function docs: https://automatewoo.com/docs/actions/custom-functions/
*
* @param $workflow AutomateWoo\Workflow
*/
function my_extend_subscription_automatewoo_action( $workflow ) {
// Valid options are: day, week, month or year
$date_period = 'month';
// E.g. extend by 3 months.
$number_of_periods = 3;
$subscription = $workflow->data_layer()->get_subscription();
$payment_date = $subscription->get_date( 'next_payment' );
if ( ! $payment_date ) {
return;
}
$payment_date_timestamp = wcs_date_to_time( $payment_date );
$new_payment_date_timestamp = wcs_add_time( $number_of_periods, $date_period, $payment_date_timestamp );
// Update the next payment date
$subscription->update_dates(
array(
'next_payment' => gmdate( 'Y-m-d H:i:s', $new_payment_date_timestamp ),
)
);
// Add note to subscription
$subscription->add_order_note(
sprintf(
'Subscription next payment date was extended from %s to %s by workflow: %s',
gmdate( wc_date_format(), $payment_date_timestamp ),
gmdate( wc_date_format(), $new_payment_date_timestamp ),
$workflow->get_title()
)
);
}
@magdakubis
Copy link

magdakubis commented Feb 7, 2021

Dan-
EDIT: code works on php. 7.1, does not work on 7.4 :(
I'd like to extend a quarterly subscription by 1 month (so when the customer purchases a quarterly subscription, their 1st recurring payment date will be 4 months out, instead of 3). However, I'd like to limit this to only new subscribers. In the Automate Woo workflow, I've added "parent" under subscription order types. Unfortunately it did not work when I tested it. Currently I have php 7.4. It worked on 7.1.
The product is a variable subscription product - I've only selected the quarterly variable for this. Is there anything I can do to limit it like that? Thank you.
Screen Shot 2021-02-07 at 10 10 58 AM

@SanderCi
Copy link

SanderCi commented May 3, 2021

Hi Dan, I don't get how you can moodify your snippet to move the 'next_payment' day to the first day of each month...
Thank's

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