• Resolved icanoop

    (@icanoop)


    I’m working on a woocommerce plugin. On WC 3.2.6

    In my code, I have a WC_Order_Item_Product with variations that I’m getting from $order->get_items(). I need to get the value of a variation. I’ve tried wc_get_order_item_meta() but it’s not returning the value. What’s the best way to get the value of a variation from an order item using the woocommerce internal (not REST) API?

Viewing 3 replies - 1 through 3 (of 3 total)
  • madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    Hi @icanoop,

    Once you have the order items list, all of them will have a get_product_id() method, items with a variation will also return a value for get_variation_id(). Here is an example of how you can use them:

    https://wordpress.stackexchange.com/questions/97176/get-product-id-from-order-id-in-woocommerce

    Cheers!

    Thread Starter icanoop

    (@icanoop)

    Hi @madeincosmos,

    Thanks but I’m trying to get the variation value, not the variation ID. For example, if I have a variation called Size and the customer chooses Small, Medium or Large when they place their order I need to get the value they chose, not the ID (returned by get_variation_id()) of the Size variation. How would I do that?

    Thanks!

    $variation = new WC_Product_Variation( $variation_id );
    $attributes = $variation->get_attributes();
    foreach( $attributes as $key => $value ) {
      if( 'pa_size' == $key ) { // use: pa_ + slug
        $size = $value;
      }
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WC Item Variation Value’ is closed to new replies.