Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the selected variation attributes from orders in Woocommerce 3

In Woocommerce, I have a report in the admin area that tallies up products sold.There are only 5 products sold on the site but there are 1 or 2 variations on some. The report works great but ignores the variations.
I need to retrieve the attribute value from items ordered to display the data accurately.
How do I do this?

get_variation_description() is not working the way I'm applying it.

My code:

$order = wc_get_order( $vs); 

//BEGIN NEW RETRIEVE ORDER ITEMS FROM ORDER 
foreach( $order->get_items() as $item_id => $item_product ){
    $ods = $item_product->get_product_id(); //Get the product ID
    $odqty= $item_product->get_quantity(); //Get the product QTY
    $item_name = $item_product->get_name(); //Get the product NAME
    $item_variation = $item_product->get_variation_description(); //NOT WORKING
}
like image 335
ComputerGiant Avatar asked Jan 22 '18 21:01

ComputerGiant


2 Answers

2020 Update - Handling "Custom Product Attributes" (revamped code)

The WC_Product method get_variation_description() is outdated and deprecated. It's replaced by get_description() method. So you need to get the WC_Product object first.

To get the selected variation attributes, you will use 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
            }
        }
    }
}

Tested and works for a product variation as all other product types…

like image 185
LoicTheAztec Avatar answered Sep 20 '22 14:09

LoicTheAztec


It works perfectly to display order item name and attributes key

foreach( $order->get_items() as $order_item_product ) {
    $item_meta_data = $order_item_product->get_meta_data();
    echo $product_name = $order_item_product->get_name();
    echo '<br>';
    foreach( $item_meta_data as $meta_data ) {
        $meta_data_as_array = $meta_data->get_data();
        echo $meta_data_as_array['key'].': '.$meta_data_as_array['value'].'<br>';
    }
}
like image 35
Muhammad tayyab Avatar answered Sep 23 '22 14:09

Muhammad tayyab