Woocommerce: get_formatted_name() of a variation

I’m trying to get the name of a variation in woocommerce, currently im using this:

$variationDetails = wc_get_product($variation_id);
$variationDetails->get_formatted_name();

But get_formatted_name() returns a long string with too many details, I only need the simple name of the variation. I’ve tried with $variationDetails->get_title() but that returns only the product title instead the variation name.

Read More

Is there a simple function to get the variation name?

Related posts

1 comment

  1. Hopefully this helps had the same problem recently this is what I wrote to get the variations title for a product

    $variations = $product->get_available_variations();
    $variation_names = array();
    
    foreach ( $variations as $variation ) {
    
    // Get attribute taxonomies
    $taxonomies = array_keys($variation['attributes']);
    
    // Loop through variation taxonomies to get variation name and slug
    foreach ($taxonomies as $tax) {
        $get_term_tax = str_replace('attribute_', '', $tax);
        $meta = get_post_meta( $variation['variation_id'], $tax, true );
        $term = get_term_by( 'slug', $meta, $get_term_tax );
        $var_name = $term->name;
        $variation_names[] = $var_name;
    }
    }
    

Comments are closed.