Updating and Synchronizing Woocommerce Subscriptions to Custom Date
There is a function exposed by the WC_Subscription
object called update_dates()
which takes an array of date keys matching the values used in the Subscriptions list dashboard (and other areas).
The function signature is WC_Subscription::update_dates( $dates, $timezone )
. I believe an object must be instantiated; I don't think this method can be called statically. Subscriptions function reference here.
The documented parameters (as keys to be passed in the $dates
array) are:
start
trial_end
next_payment
last_payment
end
The array itself is required, but I don't believe each individual key needs to be populated with a value. For instance, default orders generated by the Subscriptions plugin often have no trial_end
or end
dates (unless separately configured that way).
Using an action hook such as woocommerce_checkout_subscription_created
(Subscriptions action reference) you could use the $subscription
argument, which is an instance of WC_Subscription
, and do something like:
function push_subscriptions_to_next_week($subscription, $order, $recurring_cart) { // The new date calculation of +7 days is just an example $new_dates = array( 'start' => date('Y-m-d H:i:s', strtotime('+7 days', time())), 'next_payment' => date('Y-m-d H:i:s', strtotime('+7 days', time())) ); $subscription->update_dates($new_dates, 'site');}add_action(woocommerce_checkout_subscription_created, push_subscriptions_to_next_week, 10, 3);
The dates are required to be in a MySQL date/time format (Y-m-d H:i:s) so I've updated the code example to reflect that.