Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

php - How to get order items ids to get some product meta data?

I'm trying to extract item meta value from Woocommerce's orders by using:

$data = wc_get_order_item_meta( $item, '_tmcartepo_data', true );

However, I can't find a way to get order_item_id as the first parameter (using get_items)

global $woocommerce, $post, $wpdb;
$order = new WC_Order($post->ID);
$items = $order->get_items(); 

foreach ( $items as $item ) {
    $item_id = $item['order_item_id']; //???
    $data = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
    $a = $data[0]['value'];
    $b = $data[1]['value'];
    echo $a;
    echo $b;
}

And I mean this order item_id (1 and 2)

Order_item_id in database - Image

How can I don that?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

2018 Update:

  • Clarifying the answer with 2 possible cases
  • Added compatibility for woocommerce 3+

So There can be 2 cases:

1) Get product meta data (not set in order item meta data):

You will need to get the product ID in the foreach loop for a WC_Order and to get some metadata for this product you wil use get_post_meta() function ( but NOT wc_get_order_item_meta() ).

So here is your code:

global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items(); 

foreach ( $order->get_items() => $item ) {

    // Compatibility for woocommerce 3+
    $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $item['product_id'] : $item->get_product_id();

    // Here you get your data
    $custom_field = get_post_meta( $product_id, '_tmcartepo_data', true); 

    // To test data output (uncomment the line below)
    // print_r($custom_field);

    // If it is an array of values
    if( is_array( $custom_field ) ){
        echo implode( '<br>', $custom_field ); // one value displayed by line 
    } 
    // just one value (a string)
    else {
        echo $custom_field;
    }
}

2) Get order item meta data (custom field value):

global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items(); 

foreach ( $order->get_items() as $item_id => $item ) {

    // Here you get your data
    $custom_field = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true ); 

    // To test data output (uncomment the line below)
    // print_r($custom_field);

    // If it is an array of values
    if( is_array( $custom_field ) ){
        echo implode( '<br>', $custom_field ); // one value displayed by line 
    } 
    // just one value (a string)
    else {
        echo $custom_field;
    }
}

If the custom field data is an array, you can access the data in a foreach loop:

// Iterating in an array of keys/values
foreach( $custom_field as $key => $value ){
    echo '<p>key: '.$key.' | value: '.$value.'</p>';
} 

All code is tested and works.

Reference related to data in orders:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...