0

I'm working on Woocommerce Subscriptions. This code disables the cancel subscription button on the user account:

<?php
/**
 * Plugin Name: Remove Subscription Action Buttons from My Account
 * Plugin URI: https://gist.github.com/thenbrent/8851287/
 * Description: Remove any given button from the <a href="http://docs.woothemes.com/document/subscriptions/customers-view/#section-2">My Subscriptions</a> table on the My Account page. By default, only the "Change Payment Method" button is removed, but you can uncomment additional actions to remove those buttons also.
 * Author: Brent Shepherd
 * Author URI:
 * Version: 2.0
 */

/**
 * Remove the "Change Payment Method" button from the My Subscriptions table.
 *
 * This isn't actually necessary because @see eg_subscription_payment_method_cannot_be_changed()
 * will prevent the button being displayed, however, it is included here as an example of how to
 * remove just the button but allow the change payment method process.
 */
function eg_remove_my_subscriptions_button( $actions, $subscription ) {

    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 );

Original source: https://gist.github.com/thenbrent/8851287

But I'd like to make an exception to this: the cancel button must appear for a specific product (the 1 month subscription product)

How can I achieve that?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50

1 Answers1

0

right after your function declaration:

function eg_remove_my_subscriptions_button( $actions, $subscription ) {
// etc.

add this:

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() == 123456 ) {
            $is_my_product = true;
            break;
         }
     }
   }
   if ( $is_my_product ) return $actions;
businessbloomer
  • 1,115
  • 7
  • 11