Reputation: 11
I would like to GET the actual variation price.
The code default woocommerce (/plugins/woocommerce/templates/single-product/add-to-cart/variable.php):
<div class="woocommerce-variation-price">
{{{ data.variation.price_html }}} </div>
I how to GET this "data.variation.price_html"?
I tried this:
$pricenow = $product->get_variation_price();
<? echo $pricenow; ?>
But, only displays the lowest price...
How do I do this so that your preferred price range?
Thank you for your help!
Upvotes: 1
Views: 3425
Reputation: 100
<?php
global $product;
if ($product->is_type( 'simple' )) { ?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<?php } ?>
<?php
if($product->product_type=='variable') {
$available_variations = $product->get_available_variations();
$count = count($available_variations)-1;
$variation_id=$available_variations[$count]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
echo $regular_price+$sales_price;
}
?>
Upvotes: 0
Reputation: 21681
Please add below code to your current theme's function.php file and view the changes on front side (i.e. Variable product detail page)
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;
$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] );
return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
}
return $term;
}
Upvotes: 0