I would like to display the variable price by default in the "from price tab" in the single product page. and not have the from price.
Its confusing to the shopper to see a "from" price then below it a price for each variation, that changes once a variation is chosen.
see attached image
So instead of having to sets of price fields I want to display only one where its a single or variable product.
I would like to display the variable price by default in the "from price tab" in the single product page. and not have the from price.
Its confusing to the shopper to see a "from" price then below it a price for each variation, that changes once a variation is chosen.
see attached image
So instead of having to sets of price fields I want to display only one where its a single or variable product.
Share Improve this question asked Oct 10, 2013 at 16:19 user39262user39262 331 gold badge1 silver badge3 bronze badges 2- 1 Have you already searched the site? – kaiser Commented Oct 10, 2013 at 17:10
- I answered a very similar question to this answer here: stackoverflow/questions/43617537/… – Lucas Bustamante Commented Mar 21, 2018 at 19:08
2 Answers
Reset to default 3The function woocommerce_template_single_price()
handling the display of the "normal" price is pluggable, which means it can be overridden putting this into your functions.php
:
//override woocommerce function
function woocommerce_template_single_price() {
global $product;
if ( ! $product->is_type('variable') ) {
woocommerce_get_template( 'single-product/price.php' );
}
}
This works because in woocommerce-template.php
the functions gets initiated like this:
if ( ! function_exists( 'woocommerce_template_single_price' ) ) {
function woocommerce_template_single_price() {
woocommerce_get_template( 'single-product/price.php' );
}
}
As you can see the conditional says if the function not exists, but the function already exists. The one we put into the functions.php
will be used, because it is initiated earlier.
To show the the variation price when a single product page with a variable product is loaded, you have to select a default product variation on the product edit page.
I'm using the "Porto" Theme and haved this problem too. You can solve the problem, putting this code into your functions.php
from your child theme:
// Cheapest Price
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
$saleprice = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
Than your price looks like a single price and it the cheapest price from your variable product. It works. I've used it.