Woocommerce get variation product price

I had exactly the same question as the OP, but my case was a bit different. Here is my solution which may help others who also land on this page.

function get_product_variation_price($variation_id) {
    $product = new WC_Product_Variation($variation_id);
    return $product->product_custom_fields['_price'][0];
}

UPDATE for WooCommerce 2.2.10: The above code doesn't work on the current version of WooCommerce. This code, below, works and is worth considering as it uses the WooCommerce API, it avoids manual SQL queries and is pretty simple...

/*
 * You can find the $variation_id in the Product page (go to Product Data > Variations, and it is shown with a preceeding "#" character)
 */
function get_product_variation_price($variation_id) {

    global $woocommerce; // Don't forget this!
    $product = new WC_Product_Variation($variation_id);
    //return $product->product_custom_fields['_price'][0]; // No longer works in new version of WooCommerce
    //return $product->get_price_html(); // Works. Use this if you want the formatted price
    return $product->get_price(); // Works. Use this if you want unformatted price

}

Here is the code you are looking for

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;

    if ( empty( $term ) ) return $term;
    if ( empty( $product->id ) ) return $term;

    $id = $product->get_id();

    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;

    $query = "SELECT postmeta.post_id AS product_id
                FROM {$wpdb->prefix}postmeta AS postmeta
                    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                WHERE postmeta.meta_key LIKE 'attribute_%'
                    AND postmeta.meta_value = '$term_slug'
                    AND products.post_parent = $id";

    $variation_id = $wpdb->get_col( $query );

    $parent = wp_get_post_parent_id( $variation_id[0] );

    if ( $parent > 0 ) {
         $_product = new WC_Product_Variation( $variation_id[0] );
         return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
    }
    return $term;

}

Hope this will be useful.


This might help you guys; while searching how to do the same with the new version of WC:

global $woocommerce;
$product_variation = new WC_Product_Variation($_POST['variation_id']);
$regular_price = $product_variation->regular_price;

I'm using ajax and passing the ID of the variation with post method.