26

Im trying to display the product variation price inside the variations dropdown. Im trying to change default behavior where price is displayed inside a div when you choose a variation on the dropdown.

The problem is i cant find where that div is getting the variation price. I searched all javascript but couldnt find it

If i use :

add_filter('woocommerce_variation_option_name' ,'add_price_to_dropdown');

function add_price_to_dropdown($name){

    global $product;
    return $name.' '.$product->get_price_html();
}

I just get min variation price for all options. I want to get the price for each variation. Any clue?

chifliiiii
  • 2,231
  • 3
  • 28
  • 37

8 Answers8

46

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.

davewoodhall
  • 998
  • 3
  • 18
  • 43
Ratnakar - StoreApps
  • 4,261
  • 23
  • 17
25

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.

numediaweb
  • 16,362
  • 12
  • 74
  • 110
  • 3
    Plus 1 from me as this is so much cleaner than the accepted answer (and it showed me I needed to add `global $woocommerce;` to the code in my answer, above), but wouldn't it be more future-proof to get the price from the `WC_Product_Variation` object by referencing a function (such as `get_price()`) rather than a field (in your case, `regular_price`) as the latter is more likely to change in future versions of WooCommerce?? – ban-geoengineering Jan 06 '15 at 14:52
7

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

}
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • I've just found out, this function no longer works with the latest version of WooCommerce. I will update this code once I have the issue resolved. – ban-geoengineering Jan 06 '15 at 14:37
  • Answer updated with working code, tested with WooCommerce 2.2.10 . – ban-geoengineering Jan 06 '15 at 14:47
  • so how does this relate to original hook/filter? Where does $variation_id come from please? Want to add price next to variable options. – Sonicthoughts Nov 12 '15 at 00:23
  • I'm not sure it relates to the original hook/filter as it does have a different use case (but can be used to address the original problem). The `$variation_id` comes from the product variation settings in wp-admin. You should see it when you look at the product variations data for the product. – ban-geoengineering Nov 12 '15 at 10:21
  • Thanks a lot searching this from past 3days. now i am able to find the soltion. – Nitin Shinde Feb 21 '22 at 07:36
4

I have been using this code on Woocommerce to show my variation prices and when I updated to Woocommerce 2.0 I noticed database errors showing up in wp-admin/error-log whenever I edited a product. I am not sure if the errors were also happening prior to updating to 2.0 because I wasn't using Woocommerce for long before the update and don't think I checked the error log until today.

The variation prices showed up as expected but the error log would fill up with the following error every time I opened a product for editing. There was an error for every variation associated with the product I was editing.

WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8 for query SELECT postmeta.post_id AS product_id
            FROM wp_postmeta AS postmeta
                LEFT JOIN wp_posts AS products ON ( 

products.ID = postmeta.post_id )
            WHERE postmeta.meta_key LIKE 'attribute_%'
                AND postmeta.meta_value = '12'
                AND products.post_parent =  made by 

include('wp-admin/edit-form-advanced.php'), do_meta_boxes, call_user_func, woocommerce_product_data_box, do_action('woocommerce_product_write_panels'), call_user_func_array, variable_product_type_options, include('/plugins/woocommerce/admin/post-types/writepanels/variation-admin-html.php'), apply_filters('woocommerce_variation_option_name'), call_user_func_array, display_price_in_variation_option_name

I changed the following line in your code: AND products.post_parent = $product->id"; to this AND products.post_parent = '$product->id' ";

and now, no more errors. The error log stays nice and empty.

Just wanted to share in case anybody else ran into the problem.

Janine
  • 41
  • 1
  • 2
1

I know this is a old question but I thought I'd post my solution for anyone that wants a little more efficient and simplier solution for the problem

//Add prices to variations
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name',10,4 );

function display_price_in_variation_option_name( $term, $null, $attribute, $product) {
   $var_id = WC_Data_Store::load( 'product' )->find_matching_product_variation($product,['attribute_'.$attribute => $null->slug]);
   return ($var_id && $_product= new WC_Product_Variation($var_id)) ? $term . ' ' .wc_get_price_to_display($_product):$term;                 
} 
Breezer
  • 10,410
  • 6
  • 29
  • 50
  • this is actually throwing fatals both on product page and "order again" page .. $null is really NULL, so it can't return ->slug therefore matching_product_variation cannot work and it tries to use get_variation() on "attribute_pa_slug" which is a string – moped Apr 13 '23 at 21:38
0

My take on this issue, working in latest Woocommerce: 3.2.1 (October 2017) Price will show next to the variation in the dropdown.

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

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

    $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 = $product->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] );
        $_currency = get_woocommerce_currency_symbol();
        return $term . ' ('.$_currency.' '. $_product->get_price()  . ')';
    }
    return $term;

}

Hope this one helps someone. Add this in your child theme functions.php

0

To skip the need to create a new product object we can also use the wc_get_product() function.

Here are a couple examples:

$price = wc_get_product($product_or_variation_id)->get_price();
function get_cart_item_prices() {
  $user_cart = WC()->cart->get_cart();
  foreach ($user_cart as $cart_item) {
    $price = wc_get_product($cart_item['variation_id'])->get_price();
    echo $price;
    echo "<br/>";
  }
}
CubicInfinity
  • 163
  • 1
  • 10
0

2022

I am using $product->get_variation_prices() it returns the variation prices as well as the variation ID enter image description here