programing

Woocommerce 3의 주문 항목 보호 데이터 액세스

iphone6s 2023. 2. 21. 23:33
반응형

Woocommerce 3의 주문 항목 보호 데이터 액세스

주문의 라인 아이템을 입수하려고 합니다.

난 이걸 할 거야:

$order = new WC_Order(147);
foreach ($order->get_items() as $key => $lineItem) {
    print_r('<pre>----');
    print_r($lineItem);
    print_r('----</pre>');
}

필요한 데이터는 모두 표시되지만 어레이에는 다음과 같이 표시됩니다.

[meta_data:protected] => Array

이 어레이에 액세스하여 값을 가져오려면 어떻게 해야 합니까?

감사해요.

주문 아이템의 경우 WooCommerce 3.0+이므로 새로운 오브젝트 클래스가 있습니다.
이제 이전처럼 주문 항목 속성에 직접 액세스할 수 없습니다.

따라서 출력 원시 데이터를 보면 각 행 항목이 개체임을 알 수 있으며 다음과 같은 방법으로만 보호된 데이터에 액세스할 수 있습니다.

  1. WC_Order_Item_Product getters 메서드(또는 setters 메서드로 변경)...
  2. WC_Order_Item get_formatted_meta_data( '', true ) 모든 메타데이터에 액세스하기 위한 메서드를 지정합니다.액세스 가능한 오브젝트 배열을 제공합니다.각 메타데이터에 액세스 하는 방법을 참조해 주세요.
  3. WC_Data getters 메서드는 이 데이터의 보호를 해제하고 다음 방법을 사용하여 어레이를 통해 데이터에 액세스합니다.
    • get_data() (이 방법은 매우 유용합니다)
    • get_meta() (이 방법이 가장 편리합니다)
    • get_data_keys()
    • get_meta_data() (데이터 보호를 해제하지 않습니다.get_formatted_meta_data())
  4. wc_get_order_item_meta() 전용 기능

getters 메서드:

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

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
    echo $item->get_type().'<br>'; // The order item type
    echo $item->get_product_id().'<br>'; // The Product ID
    echo $item->get_variation_id().'<br>'; // The variation ID
    echo $item->get_quantity().'<br>'; // Line item quantity
    echo $item->get_subtotal().'<br>'; // Line item subtotal
    echo $item->get_total().'<br>'; // Line item total

    // The associated product object (which properties can't be accessed directly too)
    echo '<pre>'; print_r( $item->get_product() ); echo '</pre>'; 

    // ... and so on ...

    ## Testing raw output (protected)
    // echo '<pre>'; print_r($item); echo '</pre>';
}

기능.여기서 테이블로 이동하여 대응하는 (용)를 사용하여 항목 ID에 대한 데이터를 출력할 수 있습니다.line_item데이터 유형 항목 ID):

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

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    echo wc_get_order_item_meta( $item_id, '_product_id', true). '<br>'; // Product ID
    echo wc_get_order_item_meta( $item_id, '_variation_id', true). '<br>'; // Variation ID
    echo wc_get_order_item_meta( $item_id, '_qty', true). '<br>'; // quantity
    echo wc_get_order_item_meta( $item_id, '_line_subtotal', true). '<br>'; // Line subtotal

    // ... and so on ...

    ## Testing raw output (protected data)
    // echo '<pre>'; print_r($item); echo '</pre>';
}

방법get_data()방법(어레이 내의 데이터를 보호 해제하려면):

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

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    // Get the most useful Item product data in an accessible array
    $item_data = $item->get_data();

    echo $item_data['id'].'<br>'; // The order item ID
    echo $item_data['order_id'].'<br>'; // The order ID
    echo $item_data['product_id'].'<br>'; // The Product ID
    echo $item_data['variation_id'].'<br>'; // The Variation ID
    echo $item_data['name'].'<br>'; // The Product title (name)
    echo $item_data['quantity'].'<br>'; // Line item quantity
    echo $item_data['subtotal'].'<br>'; // Line item subtotal
    echo $item_data['total'].'<br>'; // Line item total

    // ... and so on ...

방법get_meta()method(메타 키로 각 속성에 액세스하려면):

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

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    echo $item->get_meta('_product_id').'<br>'; // The Product ID
    echo $item->get_meta('_variation_id').'<br>'; // The Variation ID
    echo $item->get_meta('_qty').'<br>'; // Line item quantity
    echo $item->get_meta('_line_subtotal').'<br>'; // Line item subtotal
    echo $item->get_meta('_line_subtotal_tax').'<br>'; // Line item subtotal tax
    echo $item->get_meta('_line_total').'<br>'; // Line item total
    echo $item->get_meta('_line_tax').'<br>'; // Line item total tax

    // Product attributes for variation
    echo $item->get_meta('pa_color').'<br>'; // Color
    echo $item->get_meta('pa_size').'<br>'; // Color

    // Custom item meta gata
    echo $item->get_meta('custom_meta_key').'<br>'; // custom meta key visible


    // ... and so on ...

관련:WooCommerce 주문 세부 정보 가져오는 방법

데이터를 가져오려면[meta_data:protected] => Array다른 방법을 사용해야 합니다.

그냥 이거 써.$item_obj->get_meta_data();

보다 상세하게 취득하려면 , 다음과 같이 2회 반복합니다.

  $order = wc_get_order( $order_id );
  foreach ($order->get_items() as $item_id => $item_obj) {

         $kua = $item_obj->get_meta_data();

         foreach ($kua as $key => $value) {
            foreach ($value as $key2 => $value2) {
               echo $key2.'->'.$value2.'<br>';
            }
        }
  }

메서드의 컬렉션은 여기에 있습니다.

언급URL : https://stackoverflow.com/questions/43597491/accessing-order-items-protected-data-in-woocommerce-3

반응형