Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Use "Starting at" prefix for variable price range
<?php
/**
* Custom variable price HTML.
* Shows "Starting at" prefix with when min price is different from max price.
*
* @param stirng $price Product price HTML
* @param WC_Product_Variable $product Product data.
* @return string
*/
function cs_my_wc_custom_variable_price_html( $price, $product ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
// Return price if min is equal to max.
if ( $min_price === $max_price || $product->is_on_sale() ) {
return $price;
}
return sprintf( __( 'Starting at %s', 'your-text-domain' ), wc_price( $min_price ) . $product->get_price_suffix() );
}
add_filter( 'woocommerce_variable_price_html', 'cs_my_wc_custom_variable_price_html', 10, 2 );
/**
* Custom variable sale price HTML.
* Shows "Starting at" prefix with when min price is different from max price.
*
* @param stirng $price Product price HTML
* @param WC_Product_Variable $product Product data.
* @return string
*/
function cs_my_wc_custom_variable_sale_price_html( $price, $product ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
$min_regular_price = current( $prices['regular_price'] );
$max_regular_price = end( $prices['regular_price'] );
// Return price if min is equal to max.
if ( $min_regular_price === $max_regular_price ) {
return $price;
}
$price = $product->get_price_html_from_to( $min_regular_price, $min_price );
return sprintf( __( 'Starting at %s', 'your-text-domain' ), $price . $product->get_price_suffix() );
}
add_filter( 'woocommerce_variable_sale_price_html', 'cs_my_wc_custom_variable_sale_price_html', 10, 2 );
@MMaKenZi

This comment has been minimized.

Copy link

@MMaKenZi MMaKenZi commented Nov 13, 2016

Hi
there is a problem

always the minimum price will be shown
even when the minimum price is for an out of stock price
we should add availability check and then use the minimum price of the available price

@mohsenamra

This comment has been minimized.

Copy link

@mohsenamra mohsenamra commented Jul 29, 2018

Hi
thx
but i want show price in variable product to : at 100$ to 250 $

how show this item?

@ssoulless

This comment has been minimized.

Copy link

@ssoulless ssoulless commented Jan 24, 2020

woocommerce_variable_sale_price_html hook is deprecated and not used anymore, if you wanna display a prefix and also display previous price when it's on sale then use the following:

add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
    $prices = $product->get_variation_prices( true );
    $min_price = current( $prices['price'] );

    $min_keys = current(array_keys( $prices['price'] ));
    $min_price_regular = $prices['regular_price'][$min_keys];
    $min_price_html = wc_price( $min_price ) . $product->get_price_suffix();

    if( $min_price_regular != $min_price ){ // When min price is on sale (Can be removed)
        $min_price_regular_html = '<del>' . wc_price( $min_price_regular ) . $product->get_price_suffix() . '</del>';
        $min_price_html = $min_price_regular_html .'<ins>' . $min_price_html . '</ins>';
    }
    $price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );

    return $price;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment