我已经添加了适用于空价格字段的代码,但我还需要它才能使用0值。
add_filter('woocommerce_empty_price_html', 'custom_call_for_price');
function custom_call_for_price() {
return 'Enquire';
}
答案 0 :(得分:1)
以下是显示价格的完整方式空和零,因此此代码也会替换您的实际代码(要删除)。
它处理简单,可变和变化的显示价格(对于空和零显示的价格):
// Variable and simple product displayed prices
add_filter( 'woocommerce_get_price_html', 'empty_and_zero_price_html', 20, 2 );
function empty_and_zero_price_html( $price, $product ) {
$empty_price = __('Enquire', 'woocommerce');
if( $product->is_type('variable') )
{
$prices = $product->get_variation_prices( true );
if ( empty( $prices['price'] ) ) {
return $empty_price; // <=== HERE below for empty price
} else {
$min_price = current( $prices['price'] );
$max_price = end( $prices['price'] );
if ( $min_price === $max_price && 0 == $min_price ) {
return $empty_price; // <=== HERE for zero price
}
elseif ( $min_price !== $max_price && 0 == $min_price ) {
return wc_price( $max_price );
}
}
}
elseif( $product->is_type('simple') )
{
if ( '' === $product->get_price() || 0 == $product->get_price() ) {
return $empty_price; // <=== HERE for empty and zero prices
}
}
return $price;
}
// Product Variation displayed prices
add_filter( 'woocommerce_available_variation', 'empty_and_zero_variation_prices_html', 10, 3);
function empty_and_zero_variation_prices_html( $data, $product, $variation ) {
if( '' === $variation->get_price() || 0 == $variation->get_price() )
$data['price_html'] = __('Enquire', 'woocommerce'); // <=== HERE for empty and zero prices
return $data;
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。