【问题标题】:How to check if a user is in a trial or not in Woocommerce Subscriptions?如何在 Woocommerce 订阅中检查用户是否处于试用期?
【发布时间】:2020-03-03 15:12:43
【问题描述】:

如果用户当前正在试用 WoocCmmerce 订阅,我如何进行简单检查以返回?

该产品的试用期为 7 天。

如果用户正在试用,我们希望在仪表板上显示一个部分,如果不是,则不应显示该部分。只是一个是或否的值。

【问题讨论】:

  • 请更清楚您使用哪个插件进行订阅。
  • @lakshmanrajput 它与 Woocommerce 订阅有关

标签: wordpress woocommerce woocommerce-subscriptions


【解决方案1】:

//试试这个

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

//返回长度1个月,7天,1年 //检查

【讨论】:

    【解决方案2】:

    我通过检查订阅是否有任何已完成且总数不为零的订单来执行此操作。如果是这种情况,则用户从未输入过付费订阅。

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

    【讨论】:

      猜你喜欢
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 2016-12-29
      相关资源
      最近更新 更多