2016-09-08 8 views
24

주문 ID별로 WooCommerce 주문 세부 정보를 가져 오는 방법은 무엇입니까? 나는 이것을 시도했지만 나를 위해 일하지 않았다.WooCommerce 주문 세부 정보를 얻는 방법

$order = new WC_Order($order_id); 
+1

이 (HTTPS에서 [문서]를 참조하십시오 ... 우리가 보호 된 데이터 (연관 배열 모드)에 액세스 할 수 있도록 .woocommerce.com/wc-apidocs/function-wc_get_order.html) – helgatheviking

+0

왜 -1입니까? 나는 모든 종류의 해결책을 시도했다. 조차 이것을 시도했습니다. $ order = new WC_Order (159); 그런 다음 해결책이 있습니까? –

+0

그 코드는 어디에 넣었습니까? 거기에 문제가 있다고 제안한 산출물은 무엇입니까? 결과물이 무엇을 기대 했습니까? 당신의 예제에서'$ order_id'는 어디에서 오는 것입니까? 사용자가 더 많은 컨텍스트가 도움이됩니다. –

답변

80

WOOCOMMERCE 주문 Woocommerce 메가 주요 업데이트 3.0 일이 꽤 많이 변경 한 이후 3.0

:

또한 참조하십시오.

하여 일부 WC_OrderWC_Abstract_Order 방법 (실시 예)

// Get an instance of the WC_Order object (same as before) 
$order = wc_get_order($order_id); 

// Get the order ID 
$order_id = $order->get_id(); 

// Get the custumer ID 
$order_id = $order->get_user_id(); 

// ... and so on ... 

얻고 (값의 배열) 순서 데이터 속성에 대한 액세스 권한 :

// Get an instance of the WC_Order object 
$order = wc_get_order($order_id); 

$order_data = $order->get_data(); // The Order data 

$order_id = $order_data['id']; 
$order_parent_id = $order_data['parent_id']; 
$order_status = $order_data['status']; 
$order_currency = $order_data['currency']; 
$order_version = $order_data['version']; 
$order_payment_method = $order_data['payment_method']; 
$order_payment_method_title = $order_data['payment_method_title']; 
$order_payment_method = $order_data['payment_method']; 
$order_payment_method = $order_data['payment_method']; 

## Creation and modified WC_DateTime Object date string ## 

// Using a formated date (with php date() function as method) 
$order_date_created = $order_data['date_created']->date('Y-m-d H:i:s'); 
$order_date_modified = $order_data['date_modified']->date('Y-m-d H:i:s'); 

// Using a timestamp (with php getTimestamp() function as method) 
$order_timestamp_created = $order_data['date_created']->getTimestamp(); 
$order_timestamp_modified = $order_data['date_modified']->getTimestamp(); 


$order_discount_total = $order_data['discount_total']; 
$order_discount_tax = $order_data['discount_tax']; 
$order_shipping_total = $order_data['shipping_total']; 
$order_shipping_tax = $order_data['shipping_tax']; 
$order_total = $order_data['cart_tax']; 
$order_total_tax = $order_data['total_tax']; 
$order_customer_id = $order_data['customer_id']; // ... and so on 

## BILLING INFORMATION: 

$order_billing_first_name = $order_data['billing']['first_name']; 
$order_billing_last_name = $order_data['billing']['last_name']; 
$order_billing_company = $order_data['billing']['company']; 
$order_billing_address_1 = $order_data['billing']['address_1']; 
$order_billing_address_2 = $order_data['billing']['address_2']; 
$order_billing_city = $order_data['billing']['city']; 
$order_billing_state = $order_data['billing']['state']; 
$order_billing_postcode = $order_data['billing']['postcode']; 
$order_billing_country = $order_data['billing']['country']; 
$order_billing_email = $order_data['billing']['email']; 
$order_billing_phone = $order_data['billing']['phone']; 

## SHIPPING INFORMATION: 

$order_shipping_first_name = $order_data['shipping']['first_name']; 
$order_shipping_last_name = $order_data['shipping']['last_name']; 
$order_shipping_company = $order_data['shipping']['company']; 
$order_shipping_address_1 = $order_data['shipping']['address_1']; 
$order_shipping_address_2 = $order_data['shipping']['address_2']; 
$order_shipping_city = $order_data['shipping']['city']; 
$order_shipping_state = $order_data['shipping']['state']; 
$order_shipping_postcode = $order_data['shipping']['postcode']; 
$order_shipping_country = $order_data['shipping']['country']; 

는 얻기 주문 항목을 확인하고 데이터에 액세스하려면 WC_Order_Item_ProductWC_Order_Item 방법 : // 문서 :

// Get an instance of the WC_Order object 
$order = wc_get_order($order_id); 

// Iterating through each WC_Order_Item_Product objects 
foreach ($order->get_items() as $item_key => $item_values): 

    ## Using WC_Order_Item methods ## 

    // Item ID is directly accessible from the $item_key in the foreach loop or 
    $item_id = $item_values->get_id(); 

    ## Using WC_Order_Item_Product methods ## 

    $item_name = $item_values->get_name(); // Name of the product 
    $item_type = $item_values->get_type(); // Type of the order item ("line_item") 

    $product_id = $item_values->get_product_id(); // the Product id 
    $wc_product = $item_values->get_product(); // the WC_Product object 
    ## Access Order Items data properties (in an array of values) ## 
    $item_data = $item_values->get_data(); 

    $product_name = $item_data['name']; 
    $product_id = $item_data['product_id']; 
    $variation_id = $item_data['variation_id']; 
    $quantity = $item_data['quantity']; 
    $tax_class = $item_data['tax_class']; 
    $line_subtotal = $item_data['subtotal']; 
    $line_subtotal_tax = $item_data['subtotal_tax']; 
    $line_total = $item_data['total']; 
    $line_total_tax = $item_data['total_tax']; 

endforeach; 

그래서 get_data() 방법을 사용하여이

+0

사용 ** get_data() **는 연관 배열을 사용하여 보호 된 데이터에 액세스하는 데 적합합니다. 이 2 개만 작동하지 않습니다 : $ order_date_created = $ order_data [ 'date_created'] -> date; $ order_date_modified = $ order_data [ 'date_modified'] -> 날짜; – Tarik

+0

@Tarik 오, 네 말이 맞아 ... 나는 내 대답을 테스트하고 업데이트했다. 이제 효과가 있습니다. 감사합니다 – LoicTheAztec

+0

또한, 나는 $ product_data = json_decode (get_product ($ item_values ​​[ 'product_id']), true)를 사용합니다; foreach 루프에서 sku : $ product_data [sku] 또는 slug : $ product_data [slug] – Tarik

40

만 WOOCOMMERCE 버전 2.5.x의와의 2.6.x위한

WOOCOMMERCE 버전은 여기

THIS UPDATE 내가 가진 사용자 정의 함수 참조 3.0 이상 들어 주문 ID의 데이터를 가져 오는 것과 관련된 일들을 명확하게하기 위해 만들어졌습니다. 당신은 당신이 얻을 수있는 모든 다른 RAW 출력을 볼 방법 print_r() 기능 사용 (또는 var_dump() 기능을 너무) 객체 또는의 원시 데이터를 출력 할 수 있도록 필요한 데이터 ...

를 얻을 수 정렬.

먼저이 데이터를 출력하여 개체 또는 배열 계층을 표시합니다. 그렇다면 변수 (문자열, 배열 또는 객체)의 유형에 따라 다른 구문을 사용하여 필요한 특정 데이터를 출력합니다.

중요 :$order 여기


는 코드 객체는 WC_order 또는 WC_Abstract_Order방법 (객체 구문을 사용하여)의 대부분을 사용할 수 있습니다
... :

function get_order_details($order_id){ 

    // 1) Get the Order object 
    $order = wc_get_order($order_id); 

    // OUTPUT 
    echo '<h3>RAW OUTPUT OF THE ORDER OBJECT: </h3>'; 
    print_r($order); 
    echo '<br><br>'; 
    echo '<h3>THE ORDER OBJECT (Using the object syntax notation):</h3>'; 
    echo '$order->order_type: ' . $order->order_type . '<br>'; 
    echo '$order->id: ' . $order->id . '<br>'; 
    echo '<h4>THE POST OBJECT:</h4>'; 
    echo '$order->post->ID: ' . $order->post->ID . '<br>'; 
    echo '$order->post->post_author: ' . $order->post->post_author . '<br>'; 
    echo '$order->post->post_date: ' . $order->post->post_date . '<br>'; 
    echo '$order->post->post_date_gmt: ' . $order->post->post_date_gmt . '<br>'; 
    echo '$order->post->post_content: ' . $order->post->post_content . '<br>'; 
    echo '$order->post->post_title: ' . $order->post->post_title . '<br>'; 
    echo '$order->post->post_excerpt: ' . $order->post->post_excerpt . '<br>'; 
    echo '$order->post->post_status: ' . $order->post->post_status . '<br>'; 
    echo '$order->post->comment_status: ' . $order->post->comment_status . '<br>'; 
    echo '$order->post->ping_status: ' . $order->post->ping_status . '<br>'; 
    echo '$order->post->post_password: ' . $order->post->post_password . '<br>'; 
    echo '$order->post->post_name: ' . $order->post->post_name . '<br>'; 
    echo '$order->post->to_ping: ' . $order->post->to_ping . '<br>'; 
    echo '$order->post->pinged: ' . $order->post->pinged . '<br>'; 
    echo '$order->post->post_modified: ' . $order->post->post_modified . '<br>'; 
    echo '$order->post->post_modified_gtm: ' . $order->post->post_modified_gtm . '<br>'; 
    echo '$order->post->post_content_filtered: ' . $order->post->post_content_filtered . '<br>'; 
    echo '$order->post->post_parent: ' . $order->post->post_parent . '<br>'; 
    echo '$order->post->guid: ' . $order->post->guid . '<br>'; 
    echo '$order->post->menu_order: ' . $order->post->menu_order . '<br>'; 
    echo '$order->post->post_type: ' . $order->post->post_type . '<br>'; 
    echo '$order->post->post_mime_type: ' . $order->post->post_mime_type . '<br>'; 
    echo '$order->post->comment_count: ' . $order->post->comment_count . '<br>'; 
    echo '$order->post->filter: ' . $order->post->filter . '<br>'; 
    echo '<h4>THE ORDER OBJECT (again):</h4>'; 
    echo '$order->order_date: ' . $order->order_date . '<br>'; 
    echo '$order->modified_date: ' . $order->modified_date . '<br>'; 
    echo '$order->customer_message: ' . $order->customer_message . '<br>'; 
    echo '$order->customer_note: ' . $order->customer_note . '<br>'; 
    echo '$order->post_status: ' . $order->post_status . '<br>'; 
    echo '$order->prices_include_tax: ' . $order->prices_include_tax . '<br>'; 
    echo '$order->tax_display_cart: ' . $order->tax_display_cart . '<br>'; 
    echo '$order->display_totals_ex_tax: ' . $order->display_totals_ex_tax . '<br>'; 
    echo '$order->display_cart_ex_tax: ' . $order->display_cart_ex_tax . '<br>'; 
    echo '$order->formatted_billing_address->protected: ' . $order->formatted_billing_address->protected . '<br>'; 
    echo '$order->formatted_shipping_address->protected: ' . $order->formatted_shipping_address->protected . '<br><br>'; 
    echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>'; 

    // 2) Get the Order meta data 
    $order_meta = get_post_meta($order_id); 

    echo '<h3>RAW OUTPUT OF THE ORDER META DATA (ARRAY): </h3>'; 
    print_r($order_meta); 
    echo '<br><br>'; 
    echo '<h3>THE ORDER META DATA (Using the array syntax notation):</h3>'; 
    echo '$order_meta[_order_key][0]: ' . $order_meta[_order_key][0] . '<br>'; 
    echo '$order_meta[_order_currency][0]: ' . $order_meta[_order_currency][0] . '<br>'; 
    echo '$order_meta[_prices_include_tax][0]: ' . $order_meta[_prices_include_tax][0] . '<br>'; 
    echo '$order_meta[_customer_user][0]: ' . $order_meta[_customer_user][0] . '<br>'; 
    echo '$order_meta[_billing_first_name][0]: ' . $order_meta[_billing_first_name][0] . '<br><br>'; 
    echo 'And so on ……… <br><br>'; 
    echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>'; 

    // 3) Get the order items 
    $items = $order->get_items(); 

    echo '<h3>RAW OUTPUT OF THE ORDER ITEMS DATA (ARRAY): </h3>'; 

    foreach ($items as $item_id => $item_data) { 

     echo '<h4>RAW OUTPUT OF THE ORDER ITEM NUMBER: '. $item_id .'): </h4>'; 
     print_r($item_data); 
     echo '<br><br>'; 
     echo 'Item ID: ' . $item_id. '<br>'; 
     echo '$item_data["product_id"] <i>(product ID)</i>: ' . $item_data['product_id'] . '<br>'; 
     echo '$item_data["name"] <i>(product Name)</i>: ' . $item_data['name'] . '<br>'; 

     // Using get_item_meta() method 
     echo 'Item quantity <i>(product quantity)</i>: ' . $order->get_item_meta($item_id, '_qty', true) . '<br><br>'; 
     echo 'Item line total <i>(product quantity)</i>: ' . $order->get_item_meta($item_id, '_line_total', true) . '<br><br>'; 
     echo 'And so on ……… <br><br>'; 
     echo '- - - - - - - - - - - - - <br><br>'; 
    } 
    echo '- - - - - - E N D - - - - - <br><br>'; 
} 

코드는 활성 자식 테마 (또는 테마)의 function.php 파일 또는 모든 플러그인 파일에 있습니다.

사용 (주문 ID 예를 들어 159 인 경우) :

get_order_details(159); 

이 코드는 테스트 및 작동된다.

년 11 월 21 일

업데이트 코드, 버전 2016

+0

내 하루를 저장했습니다. WC 2.3을 사용하는 이전 사이트에서 작업 중이므로 해당 버전의 문서를 찾을 수 없습니다. 결제 이메일을 받으려면 어떻게해야합니까? –

+0

@shadysherif 매우 행복합니다 ... 감사합니다 :) – LoicTheAztec

+1

@LoicTheAztec 당신이 저를 다시 구해 주셔서 감사합니다. 나는 단지 주문 항목 ID를 얻는 데 어려움을 겪고 있었고, 여기에 부딪쳐서 문제를 해결했습니다 ... hehehe –

관련 문제