I am trying to show/hide the ‘Cancel’ button within the subscription details in ‘My Account’, based on the subscription start date. However I am finding the interval is not working as expected. I have tried months (m) and noticed it counts whole months, not from a specific date within that month. Therefore I am trying days (d), yet the days are also not counting from the subscription start date.
For example, the subscription date of 14 Dec 2021 is more than 90 days old, however, the Cancel button still shows.
Here is the code I am using:
function remove_cancel_button( $actions, $subscription_id ) {
// Gets the subscription object on subscription id
$subscription = new WC_Subscription( $subscription_id );
// Get last payment date from subscription object, uses the sites timezone setting
$date_subscription_created = $subscription->get_date( 'start', 'site' );
$date_subscription_created = new DateTime( $date_subscription_created );
// The current date/time, uses the sites timezone setting
$today = new DateTime( current_time('mysql') );
// Get the difference in date
$interval = $today->diff( $date_subscription_created );
// Check if interval is more than * days
if( $interval->d > 90 ){
unset( $actions['cancel'] );
}
// Return the actions
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'remove_cancel_button', 100, 2);
Original source: Temporarily Remove Cancel Button if Subscription Active less than 3 months
The above code is set to read the subscription ‘start’ date and the interval is set to more than 90 days from the start date, however it is not working, any suggestions would be much appreciated!
2
Answers
Try setting lower priority to the filter for example 9
Tested OK