除非价格为零,否则在价格后添加文字,然后在WooCommerce中致电价格

除非价格为零,否则在价格后添加文字,然后在WooCommerce中致电价格

问题描述:

  1. 我的产品的价格需要以“每平方英尺”结尾。我希望这只适用于特定类别的产品,并且只在价格大于0时使用。除非价格为零,否则在价格后添加文字,然后在WooCommerce中致电价格

  2. 当产品没有列出价格时,我需要价格在没有平方英尺文字的情况下说“要求价格”之后。

所以这里是我在网上找到的开始,但他们没有任何与他们相关的条件,所以他们一直适用于所有产品。

/* Display "Call for Price" instead of empty price */ 
add_filter('woocommerce_get_price_html', 'custom_price_message'); 
function custom_price_message($price) { 
    $vat = ' per sq. ft.'; 
    return $price . $vat; 
} 

/* Display "Call for Price" instead of empty price */ 
add_filter('woocommerce_empty_price_html', 'custom_call_for_price'); 
add_filter('woocommerce_variable_empty_price_html', 'custom_call_for_price'); 
add_filter('woocommerce_variation_empty_price_html', 'custom_call_for_price'); 
function custom_call_for_price() { 
    return 'Call for Price'; 
} 
+0

另一种甜蜜的事情是有“价格面议”的文字可点击的“电话”链接,将调用一次点击。这不是必要的,但只是一个奖金 –

add_filter('woocommerce_get_price_html', 'custom_price_message'); 
function custom_price_message($price) { 
if(!empty($price)){ 
    $vat = 'Your Text Here'; 
    return $price . $vat; 
}else{ 
    return 'CALL FOR PRICE'; 
} 
} 

我希望这个作品。

+0

非常感谢。这是朝着正确方向迈出的一步,但我现在只需要添加一个条件,使其仅适用于某个产品类别。 –

+0

请投我的答案,如果有帮助@LukeBaumann –

+0

我投了票,但我的名声低于15(新帐户),所以它不会被看到,直到它更高 –

谢谢你们的帮助。我同时使用你的代码串的,这里是我想出了:

add_filter('woocommerce_get_price_html', 'custom_price_message'); 

function custom_price_message($price) { 

if (!has_term('stone', 'product_cat')) { 
    return $price; 

} elseif (!empty($price)){ 
    $vat = ' per ft<sup>2</sup>'; 
    return $price . $vat; 

}else{ 
    return 'Call for Price'; 
} 
} 
+0

对不起,帖子。我一直在努力研究如何更改上面的代码以适用于多个术语。我希望同样的规则适用于另一个产品类别“残余物”以及“宝石”。任何帮助将会很糟糕! –