Iam trying get selected variation from order.

global $order;
$items = $order->get_items();

foreach( $items as $item ) {
    $product_id = $item->get_product_id();
    $product = wc_get_product( $product_id );

    if( $product->is_type( 'variable' ) ) {
        $variationName = $product->get_attribute( 'pa_my-custom-variation' );

But this gets all variation values. I want to get only selected.

3 Answers 3


Following below code according your need

To get the selected variation attributes using get_variation_attributes( ) method.

// Get an instance of the WC_Order object from an Order ID
 $order = wc_get_order( $order_id ); 

// Loop though order "line items"
foreach( $order->get_items() as $item_id => $item ){
    $product_id   = $item->get_product_id(); //Get the product ID
    $quantity     = $item->get_quantity(); //Get the product QTY
    $product_name = $item->get_name(); //Get the product NAME

     // Get an instance of the WC_Product object (can be a product variation  too)
    $product      = $item->get_product();

     // Get the product description (works for product variation too)
    $description  = $product->get_description();

    // Only for product variation
    if( $product->is_type('variation') ){
         // Get the variation attributes
        $variation_attributes = $product->get_variation_attributes();
        // Loop through each selected attributes
        foreach($variation_attributes as $attribute_taxonomy => $term_slug ){
            // Get product attribute name or taxonomy
            $taxonomy = str_replace('attribute_', '', $attribute_taxonomy );
            // The label name from the product attribute
            $attribute_name = wc_attribute_label( $taxonomy, $product );
            // The term name (or value) from this attribute
            if( taxonomy_exists($taxonomy) ) {
                $attribute_value = get_term_by( 'slug', $term_slug, $taxonomy )->name;
            } else {
                $attribute_value = 
   $term_slug; // For custom product attributes

What you need I think is the following code:

    $get_order = wc_get_order( $order_id );
    $items     = $get_order->get_items();

    foreach ( $items as $item ) {
        $product      = $item->get_product();
        //check if this is a variation using is_type
        if ( 'variation' === $product->get_type() ) {
            $variation_id = $item->get_variation_id();
            $variation    = new WC_Product_Variation( $variation_id );
            $attributes   = $variation->get_attributes();
            foreach ( $attributes as $key => $value ) {
                if ( 'pa_my-custom-variation' === $key ) {
                    // whatever you want to do next

What it does it to look into each item from the order and check if it is actually a variable one. If it is, then it will get its variation ID and from there it will get the variation attributes and you can loop through each of them to find the one you need.

Here is also another way to check if the an item is variable. This way you are getting the variation ID if there is one.

$variation_id = $item->get_variation_id();
if(! empty($variation_id) {
// item is variable and we can check its variation

I know this is an old question but hope this might be useful for anybody who drops here.


Why get_attribute()? Maybe you're looking for get_title()? This would retrieve the variation's name.

  • I don't understand. How can i use get_title for variation?
    – wpdev
    Commented Jun 30, 2019 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.