Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocoommerce, get variation name from variation id

Tags:

woocommerce

I've used the following to get the variation id of a product in shopping cart.

    $cartdetails = $woocommerce->cart->get_cart();


foreach($cartdetails as $cart_items) {
$variation_id = $cart_items['variation_id']; //get variation id of product in cart
}

I've checked the array and the variation name isn't a value. How do i get the variation name from the variation id?

like image 686
user892134 Avatar asked Mar 23 '15 10:03

user892134


People also ask

How do I get a variation name in WooCommerce?

To get all variations ID of a variable product, we can use the below code snippet. $product = wc_get_product($product_id); $variations = $product->get_available_variations(); $variations_id = wp_list_pluck( $variations, 'variation_id' ); The above code will provide visible variation IDs only.

How do I get variation attributes in WooCommerce?

In order to get all variations, we use the $product->get_available_variations() method. This code gets all the possible WooCommerce product variations for the current $product. Thus, it is a good way to check all possibilities and their differences in terms of attributes, product price, stock.

How do I add bulk variations in WooCommerce?

Creating Product Variations in BulkHead to WooCommerce > Bulk Edit Products which will appear after enabling the plugin. To add variations for both the products at once, filter the product type “Variable(Parent)” from the dropdown. On the next page, all the parent variable products will be filtered and selected.


3 Answers

You can use the get_formatted_name() method on a product object. I believe that the object might be included in the $cart_items array, but I am not 100% sure. If not, then you can get the product and use that.

$variation = wc_get_product($variation_id);
$variation->get_formatted_name();
like image 76
helgatheviking Avatar answered Oct 19 '22 11:10

helgatheviking


Once you have the variation slug, you call wc_attribute_label().

Assuming you have access to $product, or thereabouts:

    foreach ($product->get_available_variations() as $variation) {
        foreach (wc_get_product($variation['variation_id'])->get_variation_attributes() as $attr) {
            echo '<pre>'; var_dump(wc_attribute_label( $attr )); echo '</pre>';
        }

    }
like image 5
evu Avatar answered Oct 19 '22 13:10

evu


I have looked into this a lot and could not actually find a proper solution. I tried this and it worked !

$variations = wc_get_product($product->ID);
$variation_title_pre =  $variations->get_formatted_name();
$vt = explode(" ",$variation_title_pre);
//var_dump($vt); see the exploded array
$variation_title = $vt[5].' '.$vt[6];
like image 4
MD. Atiqur Rahman Avatar answered Oct 19 '22 11:10

MD. Atiqur Rahman