Skip to content

Instantly share code, notes, and snippets.

@gonzalesc
Created December 16, 2024 18:32
Show Gist options
  • Save gonzalesc/fc73dbc5fb94ae09382211e1a76ae776 to your computer and use it in GitHub Desktop.
Save gonzalesc/fc73dbc5fb94ae09382211e1a76ae776 to your computer and use it in GitHub Desktop.
Modify the price range format on the variable product
<?php
/**
* Modify the price range format on the product page
* @package LetsGodev\MuPlugins
* @since 1.0
*/
add_filter( 'woocommerce_get_price_html', 'priceRangeFormat', 10, 2 );
/**
* Modify the price range format on the product page
* @param string $price
* @param WC_Product $product
* @return string
*/
function priceRangeFormat( string $price, WC_Product $product ): string {
if ( ! $product->is_type( 'variable' ) ) {
return $price;
}
$prices = $product->get_variation_prices( true );
$minPrice = current( $prices['price'] );
$maxPrice = end( $prices['price'] );
// Return price if min is equal to max.
if ( $minPrice === $maxPrice ) {
return $price;
}
return sprintf(
esc_html__( 'Starting at %s %s', 'letsgo' ),
wc_price( $minPrice ),
$product->get_price_suffix()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment