本文介绍了用 Woocommerce 中的自定义文本标签替换零或空显示的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我添加了这段代码,它适用于空的价格字段,但我也需要它适用于 0 的值.
I've added this code which works for empty price fields, but I also need it to work for values of 0.
add_filter('woocommerce_empty_price_html', 'custom_call_for_price');
function custom_call_for_price() {
return 'Enquire';
}
推荐答案
这是针对空和零显示价格执行此操作的完整方法,因此此代码还替换您的实际代码(将被删除).
Here is the complete way to do it for BOTH empty and zero displayed prices, so this code replace also your actual code (to be removed).
它处理简单、可变和变化的显示价格(对于空和零显示价格):
It handle simple, variable and variation displayed prices (for BOTH empty and zero displayed prices):
// 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 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于用 Woocommerce 中的自定义文本标签替换零或空显示的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!