【发布时间】:2019-07-18 17:44:59
【问题描述】:
在我们的 woocommerce 网站上,我们的价格范围是从最低价到最高价。例如 2 美元到 600 美元。然后我们对整个范围有 10% 的折扣,因此它将显示为整个范围 $2 - $600 的删除线,并显示为 $1.80 - $540。
当我现在选择一个变体时,它会显示 $540 - 例如 $0。但我只想显示 $540 并且 - $0 必须删除。我将展示一个截图示例。
我尝试了各种过滤器,但单个变体价格的实际输出没有改变。
add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {
// 1. Variable products
if( $product->is_type('variable') ){
// Searching for the default variation
$default_attributes = $product->get_default_attributes();
// Loop through available variations
foreach($product->get_available_variations() as $variation){
$found = true; // Initializing
// Loop through variation attributes
foreach( $variation['attributes'] as $key => $value ){
$taxonomy = str_replace( 'attribute_', '', $key );
// Searching for a matching variation as default
if( isset($default_attributes[$taxonomy]) && $default_attributes[$taxonomy] != $value ){
$found = false;
break;
}
}
// When it's found we set it and we stop the main loop
if( $found ) {
$default_variaton = $variation;
break;
} // If not we continue
else {
continue;
}
}
// Get the default variation prices or if not set the variable product min prices
$regular_price = $product->get_variation_regular_price( 'min', true );
$sale_price = $product->get_variation_sale_price( 'min', true );
$regular_price_max = $product->get_variation_regular_price('max', true);
$sale_price_max = $product->get_variation_sale_price( 'max', true );
}
// 2. Other products types
else {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
}
// Formatting the price
if ( $regular_price !== $sale_price && $product->is_on_sale()) {
// Percentage calculation and text
$percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
$txt = " - ";
$price = '<del>'.wc_price($regular_price).'</del><ins>'.$txt.'</ins><del>'.wc_price($regular_price_max).'</del><br><ins>'.wc_price($sale_price).$txt.wc_price($sale_price_max).'</ins>';
}
return $price;
}
add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
if( $variation->get_price() === "" ) return false;
else return true;
}
这是我目前在我的 functions.php 文件中使用的过滤器。
我希望范围变体价格与促销价一起显示,正常价格带有删除线,但在您选择变体后只显示一个价格。
【问题讨论】:
标签: php wordpress woocommerce