php 如何在Woocommerce订阅中设置下一次付款日期?

ctehm74n  于 2023-05-05  发布在  PHP
关注(0)|答案(6)|浏览(101)

我想在创建初始(第一个)订单后更改活动订阅的下一个付款日期。
当前代码不起作用。为什么我错过了什么

//fires when new order payment is complete (NOT RENEWAL but original order!)
add_action('woocommerce_payment_complete', 'fm_upate_next_payment_datetime');

function fm_upate_next_payment_datetime ( $order_id ) {

if (WC_Subscriptions_Order::order_contains_subscription( $order_id ) == false) return;      
$subscription_key = WC_Subscriptions_Manager::get_subscription_key( $order_id, $product_id
= '');

 //hard code next payment date...(must be in future)
$next_payment = date( 'Y-m-d H:i:s', '2016-05-20 00:10:10' );

//set the new payment date  
WC_Subscriptions_Manager::set_next_payment_date( $subscription_key, $user_id = '', $next_payment );

}
bsxbgnwa

bsxbgnwa1#

你说什么,我们也需要设定一个开始日期!如果没有它,它将被抛出一个错误。它是这样工作的:

function saveNewDate($post_id) {

    $subscription = wcs_get_subscription( $post_id );
    $dates        = array();

    $datetime           = current_time( 'timestamp', true );
    $dates[ 'start' ] = date( 'Y-m-d H:i:s', $datetime );
    $datetime = strtotime( "08/23/2016 23:30" );
    $dates['next_payment'] = date( 'Y-m-d H:i:s', $datetime );

    try {
        $subscription->update_dates( $dates, 'gmt' );
        wp_cache_delete( $post_id, 'posts' );
    } catch ( Exception $e ) {
        wcs_add_admin_notice( $e->getMessage(), 'error' );
    }
}
sg2wtvxw

sg2wtvxw2#

我的工作解决方案是(另见https://docs.woocommerce.com/document/subscriptions/develop/functions/#section-4):

function update_next_payment_date($subscription) {
    $date = new DateTime('2021-05-28');
    $date->setTime(8, 55);

    $subscription_next_payment_date = date_format($date, 'Y-m-d H:i:s'); // = '2021-05-28 08:55:00'

    $new_dates = array(
        'start' => $subscription->get_date('start'),
        'trial_end' => $subscription->get_date('trial_end'),
        'next_payment' => $subscription_next_payment_date,
        'last_payment' => $subscription->get_date('last_payment'),
        'end' => $subscription->get_date('end'),
    );

    $subscription->update_dates($new_dates);
}
ecbunoof

ecbunoof3#

我认为这是不起作用的,因为您使用空变量$product_id调用$subscription_key
在您的函数中,使用此代码,您没有$product_id的值。
应该通过引用或从函数中提取来传递它。

imzjd6km

imzjd6km4#

Woocommerce订阅更改了下一个付款日期3天前:

add_action('woocommerce_thankyou', 'nextpaymentdatechange', 10, 1);  

function nextpaymentdatechange( $order_id ){
    if ( wcs_order_contains_subscription( $order_id ) ) {
        $subid = $order_id + 1;
        $nextdate = get_post_meta( $subid, '_schedule_next_payment', true );
        $threedays_ago = date( 'Y-m-d H:i:s', strtotime( '-3 days', strtotime( $nextdate )) );
        update_post_meta( $subid , '_schedule_next_payment', $threedays_ago, $nextdate );
    }
}
9cbw7uwe

9cbw7uwe5#

add_action('woocommerce_thankyou', 'nextpaymentdatechange', 10, 1);  

function nextpaymentdatechange( $order_id ){
    if ( wcs_order_contains_subscription( $order_id ) ) {
        $subid = $order_id + 1;
        $nextdate = get_post_meta( $subid, '_schedule_next_payment', true );
        $threedays_ago = date( 'Y-m-d H:i:s', strtotime( '-3 days', strtotime( $nextdate )) );
        update_post_meta( $subid , '_schedule_next_payment', $threedays_ago);
    }
}

请不要在update_post_meta()中使用4个值。
通过更改以下内容仅使用3个值:

update_post_meta( $subid , '_schedule_next_payment', $threedays_ago, $nextdate );

致:

update_post_meta( $subid , '_schedule_next_payment', $threedays_ago);
a0zr77ik

a0zr77ik6#

这可能对某人有帮助。如果要更改特定产品的订阅的下次付款日期,请执行以下代码:

add_action( 'woocommerce_checkout_subscription_created', 'sw_custom_subscriptions_next_payment', 10, 3);
function sw_custom_subscriptions_next_payment( $subscription, $order, $recurring_cart ) {
  
  $order_items  = $subscription->get_items();
  $product_id   =  [];

  // Loop through order items
  foreach ( $order_items as $item_id => $item ) {
    // To get the subscription variable product ID and simple subscription  product ID
    $product_id[] = $item->get_product_id();
  }

  if ( in_array( 7856, $product_id ) ) {
    $new_dates = array(
      'next_payment' => date( 'Y-m-d H:i:s', strtotime( '+3 years', time() ) )
    );
  
    $subscription->update_dates($new_dates, 'site');
  }
}

参考:https://stackoverflow.com/a/67420195/8498688
要更改购物车和结帐页面上的首次续订日期,请执行以下步骤:

1-移除本机过滤器钩子

// Remove WCS native subscription first renewal date callback from cart/order totals on checkout page
remove_filter( 'wcs_cart_totals_order_total_html', 'wcs_add_cart_first_renewal_payment_date');

2-添加自定义回调

add_filter( 'wcs_cart_totals_order_total_html', 'sw_add_cart_first_renewal_payment_date', 10, 2 );
function sw_add_cart_first_renewal_payment_date( $order_total_html, $cart ) {

    if ( 0 !== $cart->next_payment_date ) {
    $product_id = [];
    foreach( $cart->get_cart() as $key => $cart_item ){
      $product_id[] = $cart_item['product_id'];
    }

    if ( in_array( TRIENNUAL_SUBSCRIPTION_PRODUCT_ID, $product_id ) ) {
      $first_renewal_date = date_i18n( wc_date_format(), wcs_date_to_time( '+3 years' ) );
    } else {
      $first_renewal_date = date_i18n( wc_date_format(), wcs_date_to_time( get_date_from_gmt( $cart->next_payment_date ) ) );
    }
        // translators: placeholder is a date
        $order_total_html .= '<div class="first-payment-date"><small>' . sprintf( __( 'First renewal: %s', 'woocommerce-subscriptions' ), $first_renewal_date ) . '</small></div>';
    }

    return $order_total_html;
}

相关问题