Search code examples
phpwordpressdatetimewoocommercewoocommerce-subscriptions

Hide Cancel WooCommerce Subscription button on user dashboard


I am working on a project that uses WooCommerce and the All Products for WooCommerce Subscription plugin, allowing our users to sign up for a subscription and get a discount. We would like to set up a minimum time period that the customers will have to sign up for a subscription. I did some research on doing this, and it looks like the official WooCommerce plugin simply hides the "cancel" button on the user's subscription dashboard until they've reached the designated time limit. Of course, they charge you for this functionality, and it seems like it should be very simple to just hide the cancel button until their subscription has been active for a specified amount of time, but I can't find a list of the hooks having to do with WooCommerce Subscriptions. How would I check the subscription's original activation date and then hide the cancel button based on this.

I did try this to test hiding the cancel button, but the cancel button still shows up:

add_filter('woocommerce_subscriptions_admin_order_item_actions', 'hide_cancel_button_if_less_than_3_weeks_active', 10, 2);

function hide_cancel_button($actions, $subscription) { 
    unset($actions['cancel']);

    return $actions;
}

Solution

  • You can try to use instead wcs_view_subscription_actions filter hook as follows:

    add_filter('wcs_view_subscription_actions', 'delay_user_action_cancel_button', 10, 3 );
    
    function delay_user_action_cancel_button( $actions, $subscription, $user_id ) {
        // Check that 'cancel' action button is active
        if ( ! isset($actions['cancel']) ) return $actions;
    
        $start_date = $subscription->get_date('start'); // Get the start date
        $days_period = 15; // Here define the "designated time limit" in days
    
        if ( strtotime($start_date) + ( $days_period * DAY_IN_SECONDS ) >= time() ) {
            unset($actions['cancel']);
        }
        return $actions;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It could work.