You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce中将0值或空价格替换为自定义文本标签

Solution for Handling Empty and 0 Price Values in WooCommerce

Hey there! Your current code works perfectly for empty price fields, but WooCommerce doesn’t treat a 0-value price as "empty"—so we need to adjust the logic to cover both cases.

Instead of relying only on the woocommerce_empty_price_html filter, let’s use the more flexible woocommerce_get_price_html hook. This lets us directly check the product’s price and override the display whenever it’s either empty or set to 0.

Here’s the updated code:

add_filter('woocommerce_get_price_html', 'custom_call_for_price', 10, 2);
function custom_call_for_price($price_html, $product) {
    // Fetch the product's price (works for both simple and variable products)
    $price = $product->get_price();
    
    // Check if price is empty OR explicitly set to 0
    if (empty($price) || $price == 0) {
        return 'Enquire';
    }
    
    // Return default price display if the price is valid
    return $price_html;
}

Quick breakdown of the changes:

  • We switched to woocommerce_get_price_html which gives us access to both the original price HTML and the product object.
  • The condition now checks for two scenarios: empty prices (your original use case) AND prices set to 0.
  • If neither condition is met, we just return the standard WooCommerce price display so normal products aren’t affected.

Just swap out your existing code with this snippet, and it’ll handle both empty and 0-value prices seamlessly!

内容的提问来源于stack exchange,提问作者pgilpat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:25:37