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

Get custom values of variations from order items in WooCommerce

user1932
1#
user1932 Published in July 23, 2018, 1:19 pm

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?

Edit:

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";
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO