2

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.

enter image description here

2

The 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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.