Reputation: 3214
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?
Upvotes: 11
Views: 54409
Reputation: 9
I know this is old but in more recent Woocommerce you can simply use the Product variation class
$variation_product = new WC_Product_Variation($variation_id);
$product_vari_name = $variation_product->get_name();
echo $product_vari_name;
Upvotes: -1
Reputation: 131
To show the attributes name only in variations I have looked around a bit and the following seems to show the attributes name nicely:
$product_attributes = wc_get_formatted_variation( $v_product, true, false, false );
If you want to show variation with prices in loop and single page you can use the below snippet:
function woocommerce_single_item_custom() {
global $product;
$variation_ids = $product->get_children();
if( $variation_ids ) {
foreach ( $variation_ids as $id ) {
$v_product = wc_get_product($id);
//Show only attribute name in variations title
$product_attributes = wc_get_formatted_variation( $v_product, true, false, false );
echo '<p class="variation_price">'. $product_attributes . ": ". wc_price( $v_product->get_price() ).'</p>';
}
}
}
Actions to override:
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_single_item_custom' );
add_action( 'woocommerce_share', 'woocommerce_single_item_custom' );
Upvotes: 3
Reputation: 1061
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>';
}
}
Upvotes: 5
Reputation: 462
You can get the raw value corresponding to the variation id by calling the $variation->get_variation_attributes();
method.
$variation = wc_get_product($item['variation_id']);
$variation_attributes = $variation->get_variation_attributes();
the return array $variation_attributes
will contain the corresponding variation name or value you need (for me it was the size of a shoe)
Upvotes: 5
Reputation: 2173
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];
Upvotes: 4
Reputation: 26319
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();
Upvotes: 19