WooCommerce – Use “Starting at” prefix for variable price range

By default WooCommerce display variable price ranges like:

woocommerce-variable-product-price

But it’s possible change it using filters and using the follow code:

<?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 );

view raw
functions.php
hosted with ❤ by GitHub

Example of how the price will be displayed:

woocommerce-variable-product-price-starting-at

Um comentário sobre “WooCommerce – Use “Starting at” prefix for variable price range

Os comentários estão desativados.