I'm using WooCommerce Subscriptions with a small payment gateway service and they differ in how they count a subscription interval. WooCommerce considers a monthly cycle like 15th April to 15th May, while the payment gateway considers 15th April to 14th May. That's fine except when a user tries to cancel it's subscription on, for example, 10th April. On that date, the subscription status change from "active" to "pending cancellation". The problem is that my payment gateway doesn't understand "pending cancellation". It reads only "active" or "cancelled", so on May 14th the payment gateway will charge the user again.
I would like to add an action to make Woocommerce subtract 1 or 2 days from the subscription's $end_date only when the user change his subscription from "active" to "pending cancellation". This way, when it is 14th May the status will be changed from "pending cancellation" to "cancelled" and then the gateway payment will not charge the user. I believe the action should look something like this:
add_action( 'woocommerce_subscription_status_pending-cancel', 'subtract_day_from_end_date', 10, 1 );
function subtract_day_from_end_date( $subscription, $end_date ) {
// some code here
}
Any help would be much appreciated.
EDIT: After the help from @mujuonly which unfortunately didn't work, I tried the code below, again without success:
add_filter( 'woocommerce_subscription_calculated_end_date', 'subtrai_dias_end_date' );
function subtrai_dias_end_date( $end_date, $subscription ) {
if ($end_date != 0) {
$end_date = date( 'Y-m-d H:i:s', strtotime( "-2 days", strtotime( $end_date ) ) );
} else {
$end_date = $subscription->calculate_date( 'end_of_prepaid_term' );
$end_date = date( 'Y-m-d H:i:s', strtotime( "-2 days", strtotime( $end_date ) ) );
}
$dates_to_update = array();
$dates_to_update['end'] = $end_date;
$subscription->update_dates($dates_to_update);
}
I thought that maybe the $end_date could be empty so I added the check. Also, looking at documentation I found the "update_dates" function which requires an array so I added it. Didn't work either.
Any help would be greatly appreciated.