source

WooCommerce에서 결제 직후 주문 상태 변경

lovecheck 2023. 2. 26. 09:50
반응형

WooCommerce에서 결제 직후 주문 상태 변경

결제 후 완료 주문 상태를 자동으로 변경해야 하는데, 주문 상태가 "처리 중"일 경우에만 변경되어야 합니다.그 스니펫을 발견했는데, 모든 경우에 주문 상태가 완료되었지만, 결제 변경 후 결제 플러그인은 데이터를 반환하고 "처리"를 위해 주문 상태를 변경합니다.성공 후 "완료"로 변경하고 "처리 중"이 아니면 변경하지 않고 싶습니다.제가 만난 가장 큰 문제는 받은 상태 주문을 받는 방법을 모른다는 것입니다.

코드는 다음과 같습니다.

add_filter( 'woocommerce_thankyou', 'update_order_status', 10, 2 );

function update_order_status( $order_id ) {
   $order = new WC_Order( $order_id );
   $order_status = $order->get_status();    
   if ('processing' == $order_status) {    
       $order->update_status( 'completed' );    
    }    
 //return $order_status;
}

편집:

난 이미 알아냈어.나에게 유효한 코드는 다음과 같습니다.

add_filter( 'woocommerce_thankyou', 'update_order_status', 10, 1 );

function update_order_status( $order_id ) {
  if ( !$order_id ){
    return;
  }
  $order = new WC_Order( $order_id );
  if ( 'processing' == $order->status) {
    $order->update_status( 'completed' );
  }
  return;
}

업데이트 2 - 2019:WooCommerce 사용: 유료 주문 자동 완료(업데이트된 스레드)

그래서 사용할 수 있는 그대로입니다.woocommerce_payment_complete_order_status필터 반환 완료


업데이트 1: WooCommerce 버전 3+와의 호환성

답을 바꿨습니다.

기준: WooCommerce - 자동완료 가상주문(결제방법에 따라 다름)을 기반으로 모든 결제수단도 조건부로 처리할 수 있습니다.

// => not a filter (an action hook)
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id )
        return;

    $order = new WC_Order( $order_id );

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    if ( get_post_meta($order_id, '_payment_method', true) == 'bacs' || get_post_meta($order_id, '_payment_method', true) == 'cod' || get_post_meta($order_id, '_payment_method', true) == 'cheque' ) {
        return;
     }
    // "completed" updated status for paid "processing" Orders (with all others payment methods)
    elseif ( $order->has_status( 'processing' ) ) {
        $order->update_status( 'completed' );
    }
    else {
        return;
    }
}

함수woocommerce_thankyou액션입니다.를 사용해야 합니다.add_action기능을 합니다.우선 순위를 로 변경할 것을 권장합니다.20다른 플러그인/코드 변경을 적용하기 전에update_order_status.

add_action( 'woocommerce_thankyou', 'update_order_status', 20);

언급URL : https://stackoverflow.com/questions/36969532/change-order-status-just-after-payment-in-woocommerce

반응형