WooCommerce订阅:结帐后更改续订日期

时间:2020-08-03 04:39:14

标签: php wordpress date woocommerce woocommerce-subscriptions

我有1个订阅,有3个版本。 我正在尝试根据购买的版本ID强制首次续订日期。

变化1(876)设置为每天更新-我希望第一次重新开票的日期为2020年11月15日12:00 AM 变体2(877)设置为每2天更新-我希望首次重新开票的日期为2/15/2021 12:00 AM 版本3(878)设置为每3天更新一次-我希望首次重新开票的日期为8/15/2021 12:00 AM

我认为以下代码有效。创建订单后,下一个账单日期确实会显示上述日期之一,但一定不能在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);
    }
         }
        
    }
   }
} ```

2 个答案:

答案 0 :(得分:0)

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

$subscriptions = wcs_get_subscriptions_for_order( $order_id );

根据订单ID给出订单的WC_Subscription对象的数组。

现在,您可以使用WC_Subscription method update_dates($dates),它需要设置订阅中'start''trial_end''next_payment',{{1 }}和'last_payment'

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

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

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

您可以尝试以下操作,这些操作应根据您的代码(该代码不会引发错误),从订单中更新相关的订购日期。

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

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

功能代码:

'end'

代码进入活动子主题(或活动主题)的functions.php文件中。应该可以。

答案 1 :(得分:0)

如果我明白的话,这是

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月5日的结束日期(这是简单的订阅)?