【问题标题】:Woocommerce Subscription change expiration dateWoocommerce 订阅更改到期日期
【发布时间】:2017-11-30 20:12:23
【问题描述】:

我尝试创建具有特定到期日期的订阅。

在下面的函数中, $product_args 是一个包含产品详细信息的数组。该数组包含product_id、regular_price等。product_id指的是订阅变量product。

$start_date_timestamp 是订阅开始日期。此时间戳可能是一个月前。

例如,我需要在 2 个月前(4 月 1 日)开始创建订阅。订阅期限为 3 个月,因此订阅到期日期必须为 7 月 1 日。

但现在的问题是订阅到期设置为 9 月 27 日。(因为我们目前是 6 月 27 日)

 static function create_subscription($customer_id,$product_args,$billing_args,$shipping_args = false,$order_notes = false,$client_notes = false,$order_status = false,$start_date_timestamp = false) {

        $start_date = false;
        if($start_date_timestamp==false)
        {
            $start_date = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
        }
        else
        {
            $start_date = date( 'Y-m-d H:i:s', $start_date_timestamp);
        }

        $order_id = \perfo\woocommerce\order::create_order( $customer_id, $product_args, $billing_args,$shipping_args,$order_notes,$client_notes,$order_status);

        if ( ! is_wp_error($order_id) )
        {
            $wc_product = wc_get_product($product_args['product_id']);

            $period = \WC_Subscriptions_Product::get_period( $wc_product );
            $interval = \WC_Subscriptions_Product::get_interval( $wc_product );
            $length = \WC_Subscriptions_Product::get_length( $wc_product );
            $expiration = \WC_Subscriptions_Product::get_expiration_date( $product_args['product_id'],$start_date );

            $subscription = wcs_create_subscription(array('order_id' => $order_id, 'billing_period' => $period, 'billing_interval' => $interval, 'start_date' => $start_date,'end'=>$expiration));

            if(!is_wp_error($subscription))
            {
                $wc_product->set_regular_price($product_args['regular_price']);
                $wc_product->set_price($product_args['regular_price']);
                $subscription->add_product( $wc_product, $product_args['quantity']);

                $subscription->set_address( $billing_args, 'billing' );

                if($shipping_args)
                    $subscription->set_address( $shipping_args, 'shipping' );

                $subscription->update_dates( array(
                    'end'          => $expiration,
                ) );

                $subscription->calculate_totals();
                $subscription->save();

            }
            else
            {
                wp_delete_post($order_id,true);
            }

            return $subscription;
        }

        return $order_id;
    }

【问题讨论】:

  • 请发布您用于调用此函数的代码以及传递给它的所有参数。谢谢!

标签: php wordpress woocommerce woocommerce-subscriptions


【解决方案1】:

您的结束日期必须是有效日期,并且格式必须为 'Y-m-d H:i:s'

请尝试此代码。这对我来说工作正常

$current_date = date("Y-m-d H:i:s") ;

// Setting start date to 1 day before the current date : you can set as require
$start_date = date('Y-m-d H:i:s',strtotime($current_date . "-1 days"));           

// end date : setting 12 month after the current date : you can set as require
$end_date = date('Y-m-d H:i:s',strtotime("+12 months", strtotime($start_date)));

$dates_to_update = array();
$dates_to_update['end'] = $end_date;
// $dates_to_update['next_payment'] = $end_date;
// $dates_to_update['trial_end'] = $end_date;
$subscription->update_dates($dates_to_update);

https://hotexamples.com/examples/-/WC_Subscriptions_Product/get_expiration_date/php-wc_subscriptions_product-get_expiration_date-method-examples.html

https://hotexamples.com/examples/-/-/wcs_is_datetime_mysql_format/php-wcs_is_datetime_mysql_format-function-examples.html

【讨论】:

    【解决方案2】:

    默认情况下,WooCommerce 订阅将从最后一次付款开始计算订阅的下一个付款日期。此挂钩将其更改为根据预定付款日期计算下一个付款日期,而不是实际处理付款的时间。

    要解决这个问题,请在你的 function.php 主题文件中添加这个钩子:

    add_filter( 'wcs_calculate_next_payment_from_last_payment', '__return_false' );
    

    【讨论】:

      【解决方案3】:

      如果您需要在现有产品的现有数据库中创建一些具有到期日期的新订单,那么您可以创建另一个函数来处理 $dateStart、$dateEnd 等一些参数,作为奖励,您将无需计算日期范围。

      您的第一个函数将应用于新订单。好的。 已注册订单的新订单,用于处理您的应用程序中新实现的过期机制。

      【讨论】:

        猜你喜欢
        • 2021-06-02
        • 2020-11-23
        • 2017-12-30
        • 2019-06-01
        • 1970-01-01
        • 2019-06-24
        • 2015-04-16
        • 2016-07-08
        • 2021-07-22
        相关资源
        最近更新 更多