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?
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.
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.
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.
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();
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>';
}
}
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With