Ben H
Ben H

Reputation: 512

Disable Cancel Subscription for single subscription in WooCommerce

I have 3 subscriptions available on my WooCommerce site. 1 of the subscription plans is Annual Paid Monthly. This essential means a minimum term of 12 month but paid month to month. WooCommerce doesn't natively support this.

What I would like to do is detect if the user is on a particular subscription and if so, hide the cancel button until the subscription is greater than 11 months.

I found this below that hides the cancel button in all cases. I'm looking for a way to check if the subscription is XXX and if so, hide the cancel button instead

/**
 * Only copy the opening php tag if needed
 */
function sv_edit_my_memberships_actions( $actions ) {
    // remove the "Cancel" action for members
    unset( $actions['cancel'] );
    return $actions;
}
add_filter( 'wc_memberships_members_area_my-memberships_actions', 'sv_edit_my_memberships_actions' );
add_filter( 'wc_memberships_members_area_my-membership-details_actions', 'sv_edit_my_memberships_actions' );

Upvotes: 0

Views: 947

Answers (1)

Kradyy
Kradyy

Reputation: 345

First of you need to enumerate the users current active subscriptions and detect the one we shall filter using wc_memberships_get_user_active_memberships and compare the starting date with the current date. I provided a snippet of code that might help you on the way :)

function sv_edit_my_memberships_actions( $actions ) {
        // Get the current active user
        $user_id = wp_get_current_user();

        if(!$user_id) // No valid user, abort
            return $actions;

        // Only query active subscriptions
        $memberships_info = wc_memberships_get_user_active_memberships($user_id, array( 
            'status' => array( 'active' ),
        ));

        // Loop through each active subscription
        foreach ($memberships_info as $membership) {
            $subscription_start_date = date("Y/m/d", strtotime($membership->get_start_date()));
            //$subscription_end_date = date("Y/m/d", strtotime($membership->get_end_date()));
            //$subscription_name = $membership->get_plan()->get_name();
            //$subscription_id = $membership->get_plan()->get_id();

            if($subscription_id == 'YOUR_ID') { // Active subscription
                // Compare the starting date of the subscription with the current date
                $datetime1 = date_create($subscription_start_date);
                $datetime2 = date_create(date(time()));

                $interval = date_diff($datetime1, $datetime2);

                if($interval->format('%m') <= 11) {
                    // remove the "Cancel" action for members
                    unset( $actions['cancel'] );
                }
            }
        }
       return $actions;
    }

    add_filter( 'wc_memberships_members_area_my-memberships_actions', 'sv_edit_my_memberships_actions' );
    add_filter( 'wc_memberships_members_area_my-membership-details_actions', 'sv_edit_my_memberships_actions' );

Upvotes: 3

Related Questions