programing

WooCommerce 서브스크립션 - 특정 서브스크립션 관련 주문 ID 가져오기

javajsp 2023. 2. 28. 23:15

WooCommerce 서브스크립션 - 특정 서브스크립션 관련 주문 ID 가져오기

사용자가 가지고 있는 특정 서브스크립션에 관련된 모든 주문(최소한 주문 ID)을 반환하는 woocommerce 함수가 있습니까?

이 공식 문서에는 서브스크립션 기능과 속성 참조가 기재되어 있습니다.

WC_Subscription::get_related_orders( $return_fields, $order_type );

하지만 이것은 특정 구독을 위한 것이 아닌 것 같습니까?

실행하려고 할 때마다 어떤 오류가 발생하더라도 치명적인 오류가 발생합니다.

치명적인 오류:검출되지 않은 오류: C:\xampp\htdocs\mysite.com\wp-content\plugins\woocommerce-subscriptions\class-wc-subscriptions\class-subscriptions\class-subscription에서 오브젝트 컨텍스트가 아닌 경우에 $를 사용합니다.php: 1413

자체 플러그인을 만들고 모든 구독을 선택합니다.post status = wc-active포스트 테이블에서."에 대해 알아봤습니다.woocommerce_order_items", "woocommerce_order_itemmeta" 및 "postmeta테이블은 사용자가 구입한 서브스크립션과 관련된 주문을 받을 수 있는 방법은 없습니다.

사용자가 구매한 구독과 관련 주문의 관계만 알고 있다면 sql을 쓸 수 있지만 잘 모르겠고 구글도 결과가 나오지 않습니다.

좋은 생각 있어요?

내 설정:

  • php 버전 7.0.4
  • 워드프레스 버전 4.7.3
  • woocommerce 2.6.8
  • woocommerce 서브스크립션: 2.0.18

갱신일 : WooCommerce 버전 3+ 호환성 추가

구독 개체에서 주문 ID를 매우 쉽게 얻을 수 있습니다.당신과 마찬가지로 포스트 테이블에서 모든 구독을 선택합니다.

// Get all customers subscriptions
$customer_subscriptions = get_posts( array(
    'numberposts' => -1,
    // 'meta_key'    => '_customer_user',
    // 'meta_value'  => get_current_user_id(), // Or $user_id
    'post_type'   => 'shop_subscription', // WC orders post type
    'post_status' => 'wc-active' // Only orders with status "completed"
) );

// Iterating through each post subscription object
foreach( $customer_subscriptions as $customer_subscription ){
    // The subscription ID
    $subscription_id = $customer_subscription->ID

    // IMPORTANT HERE: Get an instance of the WC_Subscription Object
    $subscription = new WC_Subscription( $subscription_id );
    // Or also you can use
    // wc_get_order( $subscription_id ); 

    // Getting the related Order ID (added WC 3+ comaptibility)
    $order_id = method_exists( $subscription, 'get_parent_id' ) ? $subscription->get_parent_id() : $subscription->order->id;

    // Getting an instance of the related WC_Order Object (added WC 3+ comaptibility)
    $order = method_exists( $subscription, 'get_parent' ) ? $subscription->get_parent() : $subscription->order;

    // Optional (uncomment below): Displaying the WC_Subscription object raw data
    // echo '<pre>';print_r($subscription);echo '</pre>';
}

포스트 쿼리 중 코멘트를 해제할 수도 있습니다.'meta_key'그리고.'meta_value'어레이 라인에서는, 1명의 고객의 서브스크립션을 취득할 수 있습니다.이 코드는 테스트되어 기능합니다.

여기서 가장 중요한 것은 다음과 같습니다.

$subscription = new WC_Subscription($customer_subscription->ID);

다음과 같은 오류 없이 모든 WC_Subscription 메서드를 적용할 수 있는 WC_Subscription 개체를 얻을 수 있습니다.

$subscription = new WC_Subscription($post_id);
$relared_orders_ids_array = $subscription->get_related_orders();

언급URL : https://stackoverflow.com/questions/42791096/woocommerce-subscriptions-get-related-orders-ids-for-a-specific-subscription