1

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.

1 Answers1

0
add_filter( 'woocommerce_subscription_calculated_end_date', 'woocommerce_subscription_calculated_end_date' );

function woocommerce_subscription_calculated_end_date( $end_date, $subscription ) {

    $end_date = date( 'Y-m-d H:i:s', strtotime( "-2 days", strtotime( $end_date ) ) );
    return $end_date;
}

enter image description here

Try this out

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • Thank you, @mujuonly . I'm afraid this code will be triggered for every new subscription, right? I want it to be run only when the user goes to his subscriptions admin page and click "cancel subscription". The end date should be changed only when the subscription status is set to "Pending cancellation". Otherwise the subscription will be cancelled even if the user do not ask for it because the subscription interval will end one day before the payment gateway charges the user. – Erick Paulino May 14 '20 at 18:20
  • I have edited the question to clarify that the days must be subtracted only when the user cancels his subscription, i.e., only when the subscription is changed from "active" to "pending cancellation". – Erick Paulino May 14 '20 at 18:32
  • @ErickPaulino AFAI this hook will trigger only when the status changing from active to pending cancel – mujuonly May 14 '20 at 23:04
  • Alright, I have just tested the code and unfortunately it didn't work. No error messages, nothing weird, just the interval remained the same. I even tried a different approach by using the function update_dates, which requires an array, but it didn't work either. If you have the time, I would really appreciate some more help. Thank you! – Erick Paulino May 15 '20 at 19:29