您可能要禁用可变产品价格范围(通常看起来像$ 100- $ 999)。使用此代码段,您可以隐藏最高价格,并在最低价格前添加“From:”。您所需要做的就是将以下代码粘贴到您的子主题的functions.php中
PHP Snippet 1:将WooCommerce可变产品价格范围$$-$$$更改为“From: $$$min_price”
add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_min', 9999, 2 );
function bbloomer_variation_price_format_min( $price, $product ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );
$price = sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $min_price ) );
return $price;
}
PHP代码段2:将WooCommerce可变产品价格范围$$-$$$更改为“From: $$$ min_reg_price $$$ min_sale_price”
add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format', 10, 2 );
function bbloomer_variation_price_format( $price, $product ) {
// 1. Get min/max regular and sale variation prices
$min_var_reg_price = $product->get_variation_regular_price( 'min', true );
$min_var_sale_price = $product->get_variation_sale_price( 'min', true );
$max_var_reg_price = $product->get_variation_regular_price( 'max', true );
$max_var_sale_price = $product->get_variation_sale_price( 'max', true );
// 2. New $price, unless all variations have exact same prices
if ( ! ( $min_var_reg_price == $max_var_reg_price && $min_var_sale_price == $max_var_sale_price ) ) {
if ( $min_var_sale_price < $min_var_reg_price ) {
$price = sprintf( __( 'From: <del>%1$s</del><ins>%2$s</ins>', 'woocommerce' ), wc_price( $min_var_reg_price ), wc_price( $min_var_sale_price ) );
} else {
$price = sprintf( __( 'From: %1$s', 'woocommerce' ), wc_price( $min_var_reg_price ) );
}
}
// 3. Return $price
return $price;
}