PHP返回值格式



你能帮助我如何自定义添加到wordpress/woocommerce中的以下代码的输出吗?

目前它显示了销售价格和常规价格,我想为它们添加一些定制,比如:着色(销售价格=红色(;文字装饰(销售价格=整行(。。

function sr_change_variable_price($price, $product) {
if ( $product->is_type( 'variable' ) && !is_product() ) 
{
return $product->get_variation_regular_price( 'min' ).' '.$product->get_variation_sale_price( 'min' ).' Ft'; // if variable product and non-product page
} else if ( $product->is_type( 'variable' ) && is_product() )
{
return '';  // if variable product and product page
} else
{
return $price;  // other cases
}
}
add_filter( 'woocommerce_get_price_html', 'sr_change_variable_price', 10, 2 );

提前非常感谢您的帮助,

在函数中,在第一个条件中连接两个字符串,在第二个条件中返回一个空字符串,在else语句中返回自价格而不做更改。因此,由于您的返回类型是字符串,因此将价格与其他字符串(如html标签(合并是没有问题的。如果这个过程对wordpress的另一个过程没有问题,你可以通过在返回中插入html标签来使用:

function sr_change_variable_price($price, $product){
$html_tag = '<span style={some styles}>%s</span>';
if($product->is_type('variable') AND !is_product()){
$min = $product->get_variation_regular_price('min') . ' ' . $product->get_variation_sale_price('min');
$min = $min ' Ft';
return(sprintf($html_tag, $min));
}elseif($product->is_type('variable') AND is_product()){
return('');
}else{
return(sprintf($html_tag, $price));
}
}
add_filter('woocommerce_get_price_html', 'sr_change_variable_price', 10, 2);

最新更新