Joey Ricketts

Removing the Cancel Button from the ‘My Account’ Page

I previously wrote a post about changing the behavior of the WooCommerce Subscriptions ‘Cancel’ button here.
The reason I’m writing a new post, rather than adding to that specific one, is due to how different I feel that this behavior change actually is.

I get many support tickets asking, “How can I hide the ‘Cancel’ button if the customer’s subscription has a subscription length set?” (now known as the ‘Expire After’ option in your product’s settings)
While this would be nice to have in Subscriptions core, it’s certainly possible to make a simple change in the meantime.

I’m going to go over the easiest way of accomplishing this behavior below.


Step 1-a: Download and install the WooCommerce Subscriptions – Hide Cancel Button Options extension.

hide cancel button options github


Step 1-b: If you’d prefer to simply paste the code from the plugin above, you should paste the following into your theme’s functions.php file:


class wcs_hide_cancel_settings {
    
    public static $option_prefix = 'woocommerce_autocomplete_orders';
    
    public static function init() {
        add_filter( 'woocommerce_subscription_settings', __CLASS__ . '::add_settings', 10, 1 );
    }

    public static function add_settings( $settings ) {

		return array_merge( $settings, array(
			array(
				'name'     => __( 'Cancel Button Options', 'woocommerce-subs-cancel-button-options' ),
				'type'     => 'title',
				'id'       => self::$option_prefix,
			),
			array(
                'name'     => __( 'Cancel Button', 'woocommerce-subs-cancel-button-options' ),
                'desc'     => __( 'Choose when you would like to hide/show the Cancel button on the My Account page.' ),
                'id'       => self::$option_prefix,
                'css'      => 'min-width:150px;',
                'default'  => 'always',
                'type'     => 'select',
                'options'  => array(
                    'always'        => _x( 'Always show', 'woocommerce-subs-cancel-button-options' ),
                    'sub_expiration'   => _x( "Hide if subscription has an 'End Date'", 'woocommerce-subs-cancel-button-options' ),
                ),
                'desc_tip' => true,
            ),
			array( 'type' => 'sectionend', 'id' => self::$option_prefix ),
		) );
        
	}
        
}
wcs_hide_cancel_settings::init();


/**
 * What to do based on the selected option from above
 */

if ( 'always' == get_option( wcs_hide_cancel_settings::$option_prefix ) ) { 
}
elseif ( 'sub_expiration' == get_option( wcs_hide_cancel_settings::$option_prefix ) ) {

    /**
     * Remove the "cancel" button.
     *
     * @param array           $actions      Array of action buttons.
     * @param WC_Subscription $subscription The subscription object.
     *
     * @return array The filtered array of actions.
     */
    function eg_remove_my_subscriptions_button( $actions, $subscription ) {
        if ( $subscription->get_time( 'end' ) === 0 || $next_payment_timestamp > $subscription->get_time( 'end' ) ) {
            return $actions;
        }
        // Hide "Cancel" button.
        unset( $actions['cancel'] );
        return $actions;
    }
    add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );
    
}
else {}


Step 2: Once the plugin has been activated (or the code pasted and saved), you’ll want to navigate to your WooCommerce Subscriptions settings: WooCommerce > Settings > Subscriptions > ‘Cancel Button Options’

settings gif


Step 3: Choose Hide if subscription has ‘End Date’ in the select dropdown.


Step 4: Navigate to the Subscriptions – ‘My Account’ page to see the results:

Subscriptions WITHOUT an ‘End Date’:

no end date

Subscriptions WITH an ‘End Date’:

end date


Once completed, you should now be seeing the behavior you’re expecting!

Let me know if this has helped or if you have any other good suggestions/questions.

Next Post

Previous Post

Leave a Reply

© 2019 Joey Ricketts

Theme by Anders Norén