Php 未正确显示退货价格
我有非常简单的代码来显示扣除后的产品价格。请参阅下面的代码Php 未正确显示退货价格,php,mysql,wordpress,function,woocommerce,Php,Mysql,Wordpress,Function,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 *
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”那样显示(它只显示最低价格)
你能帮我整理一下吗。试试这个代码:
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;
}
尝试新编辑的代码,因为旧代码不符合标准