Created
December 16, 2024 18:32
-
-
Save gonzalesc/fc73dbc5fb94ae09382211e1a76ae776 to your computer and use it in GitHub Desktop.
Modify the price range format on the variable product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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