Home Get custom values of variations from order items in WooCommerce
Reply: 1

Get custom values of variations from order items in WooCommerce

Matt B
Matt B Published in 2017-12-07 23:28:26Z

I am trying to get the variation values selected by customer at checkout from an order (by id or otherwise) to map to variables $storage and $tier which are used to build an API url.

I have tried a variety of methods to get the data onto the variables but the API url is failing, which leads me to believe I do not have the indented values on my variables.

My current code is as follows (extract):

add_action( 'woocommerce_order_status_processing', 'my_function' );
    function my_function( $order_id ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
        foreach ( $items as $key => $item ) {
                $storage = get_post_meta( $key, 'attribute_addon-storage' );
                $tier = get_post_meta( $key, 'attribute_subscription-type' );

Does anyone have any idea how I would grab the values of those two variables, addon-storage and subscription-type?

I can see the values I want to get in my database in the woocommerce_order_itemmeta table.

Can I get the values from there?


So as per LoicTheAztec's advice, the right way to get the value of the data I see in the woocommerce_order_itemmeta table as $meta_key is:

add_action( 'woocommerce_order_status_processing', 'my_function', 10, 1 );
    function my_function( $order_id ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
        foreach ( $items as $key => $item ) {
                $storage = wc_get_order_item_meta( $key, 'addon-storage', true );
                $tier = wc_get_order_item_meta( $key, 'subscription-type', true );
                $anyMetaValue = wc_get_order_item_meta( $key, '$meta_key', true );

// Then I can create the variable I need in the the API url with:

$package = "$tier$storage";
LoicTheAztec Reply to 2017-12-08 00:43:59Z

Updated: Your are confusing order post meta data and order item meta data, which are very different things and located in different database tables.

Also your code is incomplete and you will not get anything using get_post_meta() with the $key (which is the item ID)...

  • The get_post_meta( $order_id, 'meta_key', true ) function will look for Order post meta data (not related to order items) and use Order ID as argument.
  • The wc_get_order_item_meta( $item_id, 'meta_key', true ) function will look for Order item meta data realated and will use the Item ID as argument (the $key in your code)…
    So this function should need to be used in the foreach loop, where you get the order items data, instead.

Then 2 ways:

  1. You should check in your database for the last order ID you have (via phpMyAdmin) in wp_postmeta and wp_woocommerce_order_itemmeta tables, to see where is located the data…
  2. Or you can use the following code (just for testing) that will output the order items raw data where your data is located.
    This raw data will be output in the shop, archive and product pages, only visible for logged in admins. You will need to define an order ID in it.

Here is this testing function

add_action( 'woocommerce_before_main_content', 'my_testing_order_function' );
function my_testing_order_function() {

    // Define an Order ID
    $order_id = 724;

    // Only for admin user role
    if( ! current_user_can('edit_products')) return;

    $order = wc_get_order( $order_id ); // The order object

    foreach ( $order->get_items() as $item_id => $item ) {
        // Order item meta data Raw output
        echo "<pre>ORDER ITEM META DATA - (Item_id $item_id):"; print_r($item->get_data()); echo'</pre>';
        echo "<pre>ORDER ITEM META META DATA - (Item_id $item_id):"; print_r($item->get_meta_data()); echo'</pre>';

Now you have everything needed to locate and get the data. The code below should normally work for you, allowing you to get the data from your variations:

add_action( 'woocommerce_order_status_processing', 'my_function', 10, 1 );
function my_function( $order_id ) {
    $order = wc_get_order( $order_id );
    foreach ( $order->get_items() as $key => $item ) {
        // get the data
        $storage = wc_get_order_item_meta( $item_id, 'attribute_addon-storage', true );
        $tier = wc_get_order_item_meta( $item_id, 'attribute_subscription-type', true );
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303827 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO