I have this code that removes the cancel button on a WooCommerce subscription based on the product ID. I want to have it work depending on a whether a custom field is true/false. The custom field is: $remove_cancel = get_field(‘remove_cancel’);
The code that works (I’ve tested) using the product ID is:
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
$is_my_product = false;
$specific_products = array( 134 );
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 );
Any idea on how to make it work with a custom field the user toggles on the product for true/false?