cactusboat
cactusboat

Reputation: 766

Temporarily Remove Cancel Button if Subscription Active less than 3 months

I am in need of some help temporarily removing the cancel button in the "My Subscription" page within "My Account". I would like to hide the cancel button until it has been at least 3 months (or 90days) since the user has subscribed. Where after they have been subscribed for 3 months, the cancel button will appear again.

Using: Woocommerce together with Woo-subscriptions and Woo-memberships

I have found another question where this has been answered, but I cannot seem to get the code to work no matter how much I edit it (Disable Cancel Subscription for single subscription in WooCommerce). The first snippet of code below is from the link.

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

I have been able to hide the cancel button with my code below, however it hides it indefinitely:

function remove_cancel_button( $actions, $subscription ) {

        foreach ( $actions as $action_key => $action ) {
          switch ( $action_key ) {
            case 'cancel':          // Remove the cancel button
                unset( $actions[ $action_key ] );
                break;
            default: 
                error_log( '-- $action = ' . print_r( $action, true ) );
                break;
          }
        }

        return $actions;
    }
    add_filter( 'wcs_view_subscription_actions', 'remove_cancel_button', 100, 2);

Upvotes: 2

Views: 1665

Answers (1)

Lennart
Lennart

Reputation: 659

I've read the related developer documentation found here and altered your code so that it uses the current sites date and compares it to the subscription date.

If there is less then a 3 months difference the cancel button will stay hidden until there is a at least a 3 month difference.

Note that I used 'last_payment' date to compare with, other possible options to use are 'start', 'trial_end', 'next_payment', 'last_payment' or 'end'. Read more about it here.

/**
 * Remove cancel button ( When last payment was less then 3 months ago )
 *
 * @param array $actions, action array.
 * @param int $subscription_id, the id of the current subscription.
 * @return array $actions, the altered action array.
 */
function remove_cancel_button( $actions, $subscription_id ) {

  // Gets the subscription object on subscription id
  $subscription = new WC_Subscription( $subscription_id );

  // Get last payment date from subscription object, uses the sites timezone setting
  $last_payment_date = $subscription->get_date( 'last_payment', 'site' );
  $last_payment_date = new DateTime( $last_payment_date );

  // The current date/time, uses the sites timezone setting
  $today = new DateTime( current_time('mysql') );

  // Get the difference in date
  $interval = $today->diff( $last_payment_date );

  // Check if interval is less then 3 months
  if( $interval->m < 3 ){
    unset( $actions['cancel'] );
  }

  // Return the actions
  return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'remove_cancel_button', 100, 2);

Hopefully this helps you out, let me know if anything is unclear.

Upvotes: 5

Related Questions