Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce get variation product price

Tags:

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?

like image 389
chifliiiii Avatar asked Sep 04 '12 23:09

chifliiiii


2 Answers

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.

like image 105
Ratnakar - StoreApps Avatar answered Jan 16 '23 19:01

Ratnakar - StoreApps


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.

like image 42
numediaweb Avatar answered Jan 16 '23 19:01

numediaweb