programing

우커머스 서브스크립션에서 다음 결제일을 설정하는 방법은?

easyjava 2023. 9. 26. 22:40
반응형

우커머스 서브스크립션에서 다음 결제일을 설정하는 방법은?

Initial(첫번째) 주문이 생성된 후에 활성가입의 다음 결제일을 변경하고 싶습니다.

현재 코드가 작동하지 않습니다. 왜죠?제가 무엇을 빠뜨리고 있나요?

//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 );

}

말씀하신 내용과 시작 날짜를 설정해야 합니다! (NOW일 수 있음) 하지만 통과되지 않으면 오류가 발생합니다.다음과 같이 작동합니다.

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' );
    }
}

작업 솔루션은 다음과 같습니다(https://docs.woocommerce.com/document/subscriptions/develop/functions/ #섹션-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);
}

당신이 전화를 하고 있기 때문에 이것은 작동이 안 되는 것 같습니다.$subscription_key빈 변수 사용$product_id.
당신의 기능에서, 이 코드를 사용하면, 당신은 다음에 대한 값이 없습니다.$product_id.

참조하거나 함수에서 추출하여 전달해야 합니다.

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 );
    }
}
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);
    }
}

4개의 값을 사용하지 말아주세요.update_post_meta().

이 값을 변경하여 3개의 값만 사용합니다.

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

다음으로:

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

이것은 누군가에게 도움이 될 지도 모릅니다.특정 제품에 대한 구독의 다음 지불 날짜를 변경하려면 다음 코드를 따르십시오.

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

언급URL : https://stackoverflow.com/questions/37068851/how-to-set-next-payment-date-in-woocommerce-subscriptions

반응형