I am trying to show/hide the 'Cancel' button within the subscription details in 'My Account', based on the subscription start date. The way it should run is the button should not be visible for 11 months from the start of the subscription, but after the 11th month it should appear and give the customer a 1 month window in which to cancel their subscription. Once they hit the 12 month mark, the button should then disappear again for another 11 month.
I was using 'Custom Cancellation Rules' but it seems very buggy, and I'd rather not use a plugin if we can help it. I've figured out how to hide the cancel button entirely using the following code, but can't figure out how to set up the required schedule.
This is some code I've tested that seems to successfully remove the cancel button, but it doesn't have the logic of hiding for 11 months, showing for 1 and then hiding again.
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
$is_my_product = false;
$specific_products = array( 496875,254616 );
if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
foreach ( $subscription_items as $item_id => $item ) {
$product = $item->get_product();
if ( in_array( $product->get_id(), $specific_products ) ) {
$is_my_product = true;
break;
}
}
}
if ( !$is_my_product ) return $actions;
foreach ( $actions as $action_key => $action ) {
switch ( $action_key ) {
case 'change_payment_method': // Hide "Change Payment Method" button?
// case 'change_address': // Hide "Change Address" button?
case 'switch': // Hide "Switch Subscription" button?
case 'resubscribe': // Hide "Resubscribe" button from an expired or cancelled subscription?
case 'pay': // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
case 'reactivate': // Hide "Reactive" button on subscriptions that are "on-hold"?
case 'cancel': // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
unset( $actions[ $action_key ] );
break;
default:
error_log( '-- $action = ' . print_r( $action, true ) );
break;
}
}
return $actions;
} add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );