Reputation: 981
We have a situation in a Woocommerce Subscriptions set up where the default sync options won't work. I've created a manual calendar that my functions.php
file references at subscription creation time, but it only ever checks at subscription creation and then the plugin automatically sets the next renewal to be every 4 weeks thereafter.
I need to fire my renewal date checker function after each subscription creation and/or renewal to calculate the next renewal date from my calendar. I know there are at least two hooks I could try, being:
wcs_renewal_order_created
and woocommerce_subscription_payment_complete
, but I don't know enough about when those are triggered to know which is the safest.
About woocommerce_subscription_payment_complete
, the WC Subscriptions documentation says:
Triggered when a payment is made on a subscription. This can be payment for the initial order or a renewal order. It is important to note, however, that subscription date changes related to early renewals are processed differently than other renewals, so this action should not be relied upon for capturing the next payment date on a subscription.
I'm not 100% sure what case they refer to in their note, but it was enough to give me pause.
About wcs_renewal_order_created
, I am unclear as to when this fires:
These orders are always created through the wcs_create_renewal_order() function, regardless of whether they are created for a scheduled renewal event, manually via the WooCommerce > Edit Subscription administration screen, or via the Subscriptions endpoints for the WooCommerce REST API. Because of this, it’s possible to add, remove or update the value of anything on that renewal order using this filter.
For example, this can be used to add a discount to specific renewal orders, like the 12th order each year. It could also be used to add one-time fee for a certain renewal order, like a special annual extra fee on a monthly subscription.
Would trying to set the next_date
key via update_dates()
during this hook push the current payment it was about to process or has it already happened and so setting the next date is now safe?
What is the most reliable way to set the next renewal date programmatically immediately after the current period has been processed for a given subscription?
Upvotes: 4
Views: 2045
Reputation: 10398
Disclaimer: I share my reasoning and preliminary information, I did not do any practical check.
Based on the description, both hooks are not related to the events that are required: wcs_renewal_order_created
responds to the creation of a subscription order, and woocommerce_subscription_payment_complete
responds to payment for such an order. But both of these points are responsible for the "bookkeeping rationale" of the creating or renewing a subscription, not for the creation or renewal itself.
I suggest testing the woocommerce_helper_subscription_activate_success
hook, which can be found in the class-wc-helper.php:
/**
* Active a product subscription.
*/
private static function _helper_subscription_activate() {
// ...
if ( $activated ) {
/**
* Fires when the Helper activates a product successfully.
*
* @param int $product_id Product ID being activated.
* @param string $product_key Subscription product key.
* @param array $activation_response The response object from wp_safe_remote_request().
*/
do_action( 'woocommerce_helper_subscription_activate_success', $product_id, $product_key, $activation_response );
} else {
// ...
It looks like this hook responds to the moment when the subscription is successfully activated. And there is a possibility that this means not only the moment of the initial activation of the subscription, but also its subsequent renewals.
This can be done with the following code, for example:
function test_woocommerce_helper_subscription_activate_success( $product_id, $product_key, $activation_response ) {
// your code here
}
add_action('woocommerce_helper_subscription_activate_success', 'test_woocommerce_helper_subscription_activate_success', 10, 3);
Upvotes: 2