【问题标题】:WooCommerce Subscriptions: change renewal dates after checkoutWooCommerce 订阅:结帐后更改续订日期
【发布时间】:2020-11-23 04:31:20
【问题描述】:

我有 1 个订阅,包含 3 个变体。 我正在尝试根据购买的变体 ID 强制第一个续订日期。

变体 1 (876) 设置为每天更新 - 我希望第一个重新计费日期为 2020 年 11 月 15 日上午 12:00 变体 2 (877) 设置为每 2 天更新一次 - 我希望第一个重新计费日期为 2021 年 2 月 15 日上午 12:00 变体 3 (878) 设置为每 3 天更新一次 - 我希望第一个重新计费日期为 2021 年 8 月 15 日上午 12:00

我认为下面的代码有效。创建订单后,下一个账单日期会显示上述日期之一,但不能是在 WooCommerce 或其他地方注册,因为无论上述日期如何,都会触发续订。

对于我的测试,我为变体 1 创建了一个订单,下一个付款日期显示为 2020 年 11 月 15 日,但它会在第二天续订。

希望能从比我聪明的人那里得到一些见解。同样,潜艇的下一个账单日期显示上述日期,但续订仍会提前/在上述日期之前进行。


function nextpaymentdatechange( $order_id ){
    $order = wc_get_order( $order_id );
$items = $order->get_items(); 
foreach ( $items as $item_id => $item ) {
   $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
   if ( $product_id === 876 ) {
        $subid = $order_id + 1;
        $nextdate = get_post_meta( $subid, '_schedule_next_payment', true );
        $new_date = date( 'Y-m-d H:i:s', strtotime( '2020-11-15 07:00:00', strtotime( $nextdate )) );
        update_post_meta( $subid , '_schedule_next_payment', $new_date);
   }
    else{ if ( $product_id === 877 ) {
        $subid = $order_id + 1;
        $nextdate = get_post_meta( $subid, '_schedule_next_payment', true );
        $new_date = date( 'Y-m-d H:i:s', strtotime( '2021-02-15 07:00:00', strtotime( $nextdate )) );
        update_post_meta( $subid , '_schedule_next_payment', $new_date);
    }
         else{
              if ( $product_id === 878 ) {
        $subid = $order_id + 1;
        $nextdate = get_post_meta( $subid, '_schedule_next_payment', true );
        $new_date = date( 'Y-m-d H:i:s', strtotime( '2021-08-03 07:00:00', strtotime( $nextdate )) );
        update_post_meta( $subid , '_schedule_next_payment', $new_date);
    }
         }
        
    }
   }
} ```

【问题讨论】:

  • 你有解决这个问题的办法吗?

标签: php wordpress date woocommerce woocommerce-subscriptions


【解决方案1】:

要完成这项工作,您首先需要使用以下命令从订单中获取 WC_Subscription 对象:

$subscriptions = wcs_get_subscriptions_for_order( $order_id );

根据订单 ID 提供订单的 WC_Subscription 对象数组。

现在您可以使用WC_Subscription method update_dates($dates),这需要设置来自订阅的所有日期类型的数组,这些日期类型是'start''trial_end''next_payment''last_payment''end'

要从订阅中获取特定日期,我们使用WC_Subscription method get_date($date_type)

另外我使用WC_Subscription method can_date_be_updated($date_type),检查日期类型是否可以更新。

我不确定哪些日期需要更新,因为它们可能相关。

您可以尝试以下操作,根据您的代码更新订单中的相关订阅日期(代码不会引发错误)

我认为您需要更改 'next_payment''last_payment''end' 日期。

使用代码末尾的 update_dates() 和 save() 方法,可以更改日期,保存所有需要的数据到数据库刷新缓存数据。

功能代码:

function nextpaymentdatechange( $order_id ){
    // YOUR SETTINGS: Set in this array your desired dates (value(s)) by product Id (key)
    $dates_for_product = array(
        876 => array(
            'next_payment' => '2020-11-15 07:00:00',
            'last_payment' => '2020-11-16 07:00:00',
        ),
        877 => array(
            'next_payment' => '2021-02-15 07:00:00',
            'last_payment' => '2021-02-16 07:00:00',
        ),
        878 => array(
            'next_payment' => '2021-08-03 07:00:00',
            'last_payment' => '2021-08-04 07:00:00',
        ),
    );

    // The date types for subscriptions
    $suscription_date_types = array('start', 'trial_end', 'next_payment', 'last_payment', 'end');

    // Get the subscriptions from the current order Id
    $subscriptions = wcs_get_subscriptions_for_order( $order_id );

    // Loop through subscriptions
    foreach ( $subscriptions as $subscription_id => $subscription ) {
        // Loop through items in this subscription
        foreach ( $subscription->get_items() as $item_id => $item ) {
            $product = $item->get_product();

            // Loop through defined products dates
            foreach( $dates_for_product as $product_id => $new_dates ) {
                // If current subscription product id matches
                if ( $product->get_id() == $product_id ) {
                    $current_product_id = $product_id; // Set current product id
                    break; // Stop the current loop
                }
            }
            if ( isset($current_product_id) ) {
                break; // Stop the current loop
            }
        }

        // Updating subscription dates
        if ( isset($current_product_id) ) {
            $updated_dates = array(); // Initializing

            // Loop through subscription date types
            foreach( $suscription_date_types as $date_type ) {
                $date           = $subscription->get_date($date_type);

                // For 'next_payment' and 'last_payment' dates
                if( isset($new_dates[$date_type]) && $subscription->can_date_be_updated($date_type) ) {
                    $updated_dates[$date_type] = $new_dates[$date_type];
                }
                // For 'end' date
                elseif ( $date_type === 'end' && $subscription->can_date_be_updated($date_type) ) {
                    $updated_dates[$date_type] = $new_dates['last_payment']; // ??? Or may be instead: $date;  … (or simply: 0;)
                }
                // Other dates
                else {
                    $updated_dates[$date_type] = $date;
                }
            }

            // Update subscription date, save to database and refresh cached data
            $subscription->update_dates($updated_dates);
            $subscription->save();
        }
    }
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

【讨论】:

  • 很遗憾,我无法让它工作。我将代码放在我的测试站点上的 functions.php 中,但 id 没有工作。 monthlycase.com
  • @LoicTheAztec 我之前在 WCS 开发文档中没有看到 save() 方法,但我仍然找不到对它的引用。使用update_dates() 后是否总是需要这样做?我认为我的初始测试在没有的情况下可以正常工作,但如果有人推荐,我肯定会添加它。
【解决方案2】:

如果我明白了,这个:

function nextpaymentdatechange( $order_id ){
// YOUR SETTINGS: Set in this array your desired dates (value(s)) by product Id (key)
$dates_for_product = array(
    588 => array(
        'end' => '2020-12-05 07:00:00',
    ),
);

// The date types for subscriptions
$suscription_date_types = array('start', 'trial_end', 'next_payment', 'last_payment', 'end');

为我的产品 588 自动设置结束日期 12 月 05 日(这是一个简单的订阅)?

【讨论】:

  • 点评来源: 你能澄清一下这是否是问题的答案吗?请编辑它并使回答部分更明显。
猜你喜欢
  • 2021-06-02
  • 2017-11-30
  • 2019-06-24
  • 2021-05-17
  • 2018-09-19
  • 2018-04-04
  • 1970-01-01
  • 2017-12-30
  • 2020-02-07
相关资源
最近更新 更多