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()
)
);
}
@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