在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文件中使用的过滤器。
我希望范围变化价格与销售价格一起显示,而常规价格有删除线,但是一旦选择变化,就仅显示一个价格。
答案 0 :(得分: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 = " - ";
//Check if Sale Price Exists
if(wc_price($sale_price_max) !== 0.00){
$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>';
} else {
//else return price without second sale price
$price = '<del>'.wc_price($regular_price).'</del><ins>'.$txt.'</ins><del>'.wc_price($regular_price_max).'</del><br><ins>'.wc_price($sale_price).'</ins>';
}
}
return $price;
}
请尝试上面的代码,并让我知道。