自 WooCommerce 3 以来,woocommerce_variable_sale_price_html 钩子已被弃用,不再有用。如果你不关心“最低”售价(最低售价时),你可以使用这个:
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_price_html = wc_price( $min_price ) . $product->get_price_suffix();
$price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );
return $price;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
在 WooCommerce 3+ 上测试并运行。你会得到这样的东西:
如果您关心“最低”销售价格(当最低价格打折时),并且您想同时显示这两个价格,您应该使用以下代码:
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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
在 WooCommerce 3+ 上测试并运行。你会得到这样的东西:
在所有变化价格相同时处理:
WooCommerce variable products: Display the min price with a custom text for different prices