1

How can I do a simple check to return if a user is currently on a trial with WoocCmmerce subscriptions?

The product has a trial period of 7-days.

We want to display a section on the dashboard if the user is on trial and if they are not, the section should not display. Just a yes or no value.

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
LinuxGoose
  • 11
  • 1

2 Answers2

0

I do this by checking if the subscription has any completed orders with a total not zero. If this is the case the user has never entered a paid subscription.

/**
 * Checks whether subscription is in trial. This will also be true for an expired trial, before renewal.
 *
 * @param   null|object $sub  WC Subscription object to check.
 *
 * @return  bool        True if subscription is in (expired) trial.
 */
 function in_trial( $sub ) {
    $related_orders = $sub->get_related_orders();

    $in_trial = true;

    if ( ! empty( $related_orders ) ) {
        foreach ( $related_orders as $related_order_number ) {
            $curr_order = wc_get_order( $related_order_number );

            if (
                'completed' === $curr_order->get_status() &&
                0 !== (int) $curr_order->get_total()
            ) {
                $in_trial = false;
                break;
            }
        }
    }
    return $in_trial;
}
Punchlinern
  • 714
  • 5
  • 17
  • 33
-1

// Try this

 $subscription_id = 5422;
     $subscription = wcs_get_subscription($subscription_id);

     $get_length = $subscription->get_trial_length(); //1 get_post_meta($subscription_id, '_subscription_trial_length', true);
     $get_period = $subscription->get_trial_period(); // month get_post_meta($subscription_id, '_subscription_trial_period', true);

if(!empty($get_length) && !empty($get_period )){
 // code here
}

//return leength 1 month, 7 days, 1 year //check for