我有非常简单的woocommerce代码来显示扣除后的产品价格。请参阅以下代码。
add_action( 'woocommerce_get_price_html' , 'rp_get_price' );
function rp_get_price($price){
$rp_percentage = 10;
$regular_price = get_post_meta( get_the_ID(), '_price', true );
$rp_discount = $regular_price * $rp_percentage / 100;
$price = $regular_price - $rp_discount;
return $price;
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
$rp_percentage = 10;
$min_price = $product->get_variation_price( 'min', true );
$max_price = $product->get_variation_price( 'max', true );
$rp_min_discount = $min_price*10/100;
$rp_min_price = $min_price - $rp_min_discount;
$rp_max_discount = $max_price*10/100;
$rp_max_price = $max_price - $rp_max_discount;
if ($min_price != $max_price)
{
$price = $rp_min_price . '-' . $rp_max_price;
}
else
{
$price = $rp_min_price;
}
return $price;
}
对于单品,价格显示正确。对于具有相同最小和最大费率的变化产品显示正确但我的问题是变化价格低价格到高价格它没有显示像'20 -30'(它只显示最低价格) 你能帮我解决一下吗?
答案 0 :(得分:0)
试试这段代码:
add_action( 'woocommerce_get_price_html' , 'rp_get_price' );
function rp_get_price($price){
global $product;
//get the sale price of the product whether it be simple, grouped or variable
$sale_price = get_post_meta( get_the_ID(), '_price', true);
//get the regular price of the product, but of a simple product
$regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
$rp_percentage = 10;
if( $product->is_type( 'simple' ) ):
$rp_discount = $regular_price * $rp_percentage / 100;
$price = $regular_price - $rp_discount;
else:
if ($regular_price == ""){
$min_price = $product->get_variation_price( 'min', true );
$max_price = $product->get_variation_price( 'max', true );
$rp_min_discount = $min_price*10/100;
$rp_min_price = $min_price - $rp_min_discount;
$rp_max_discount = $max_price*10/100;
$rp_max_price = $max_price - $rp_max_discount;
if ($min_price != $max_price)
{
$price = $rp_min_price . '-' . $rp_max_price;
}
else
{
$price = $rp_min_price;
}
}
endif;
return $price;
}
<强>编辑:强>
add_action( 'woocommerce_get_price_html' , 'rp_get_price' );
function rp_get_price($price){
global $product;
//get the sale price of the product whether it be simple, grouped or variable
$sale_price = get_post_meta( get_the_ID(), '_price', true);
//get the regular price of the product, but of a simple product
$regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
$rp_percentage = 10;
if ($regular_price == ""){
$currency = get_woocommerce_currency_symbol();
$min_price = $product->min_variation_price;
$max_price = $product->max_variation_price;
$rp_min_discount = $min_price*10/100;
$rp_min_price = $min_price - $rp_min_discount;
$rp_max_discount = $max_price*10/100;
$rp_max_price = $max_price - $rp_max_discount;
if ($min_price != $max_price)
{
$price = $rp_min_price . '-' . $rp_max_price;
}
else
{
$price = $rp_min_price;
}
}
else{
$rp_discount = $regular_price * $rp_percentage / 100;
$price = $regular_price - $rp_discount;
}
return $price;
}
尝试使用新编辑的代码,因为旧编码不符合标记。