I've a list of products and I've a problem about displaying the range of prices in variable products.
As you can see if the price is a normal price everything works. Nevertheless if the product has a variable price the price displayed is 0€
Well, the code for displaying price is this:
<?php
if (isset($ebookstore_theme_option['woo-list-price']) &&
$ebookstore_theme_option['woo-list-price'] == 'enable') {
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$price_sale.'</strong>';
}
Any idea about how to display the variable range price?
For a variable product in WooCommerce you need:
So you should need to get an instance of the product object. In general you can get it with:
global $product;
If it doesn't work you will use intead:
global $post;
$product = wc_get_product( $post->ID );
Now from this point you will use the following:
<?php
global $product;
if ( isset( $ebookstore_theme_option['woo-list-price'] ) && $ebookstore_theme_option['woo-list-price'] == 'enable' ){
// For all product types that are not "variable products"
if( ! $product->is_type('variable') ){
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$price_sale.'</strong>';
}
// For variable products
else {
$min_price = $product->get_variation_price( 'min' );
$max_price = $product->get_variation_price( 'max' );
$ebookstore_woo .= '<strong class="amount">'.esc_attr($currency).''.$min_price.' - '.esc_attr($currency).''.$max_price.'</strong>';
}
}
As you can see I am using the WC_Product_Variable
methods
May be you don't know, but woocommerce has several formatting price functions as:
wc_price( $price );
wc_format_price_range( $from, $to );
wc_format_sale_price( $regular_price, $sale_price );
They will all include the currency