2

Based on that question: Disable cancel subscription for a specific product in WooCommerce we try to adjust the code becasue this code here does not 100% fit for our needs.

We created the code below to hide subscription buttons on myaccount page but only for specific subscription products. For all variation from a variable product. For that we want work with a array to define all variation ID's. But I think it will also work, if we just add the objekt ID from the product and for every variation. However, this is what I have so far. Currently it hides the buttons for every subscription. So I think there is a logic issue in my code.

Its strange.. when I set the ID then it somehow hide the buttons for the other products and not for the product with the specific ID. So the code is applied to all other products and not to the one with the exact ID. Why?

function eg_remove_my_subscriptions_button( $actions, $subscription ) {
   $is_my_product = false;
   if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
     foreach ( $subscription_items as $item_id => $item ) {
         $product = $item->get_product();
         if ( $product->get_id() == 16189, 16190 ) {
            $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 whats wrong here? :)

Cheers & Thanks

Nik7
  • 346
  • 2
  • 16

1 Answers1

3

First, there is error within this line if ( $product->get_id() == 16189, 16190 ) {. You are looking for is in_array function.

Define your specific product ids array.

$specific_products = array( 16189, 16190 );

Then add a condition whether your product is in your define $specific_products using the in_array function.

if ( in_array( $product->get_id(), $specific_products ) ) {

also you have to use woocommerce_my_account_my_orders_actions filter hook.

Complete code for 'woocommerce_my_account_my_orders_actions' filter hook.

function eg_remove_my_subscriptions_button( $actions, $subscription ) {

    $is_my_product = false;
    $specific_products = array( 16189, 16190 );
    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( 'woocommerce_my_account_my_orders_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

Before

enter image description here

After

enter image description here

Complete code for 'woocommerce_my_account_my_orders_actions' filter hook.

function eg_remove_my_subscriptions_button( $actions, $subscription ) {

    $is_my_product = false;
    $specific_products = array( 16189, 16190 );
    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 );

Before

enter image description here

After

enter image description here

Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • Thanks. But when I change the ID's for my products it does not hide the buttons. But now the buttons are always visible. So it seems that the code does not work on my system. Is there a special setting in the variation which I have to set or can check? – Nik7 Feb 14 '22 at 16:28
  • Ah, I found the issue.. I have to use wcs_view_subscription_actions becasue its related here to subscriptions and not the order it self :) – Nik7 Feb 14 '22 at 16:30
  • Welcome... glad to be of help. – Bhautik Feb 14 '22 at 17:46