在价格后添加文本,除非价格为零,然后在 WooCommerce 中调用价格

Add text after price unless the price is zero, then Call for price in WooCommerce

  1. 我有价格需要全部以 "per sq. ft." 结尾的产品我希望这仅适用于特定类别的产品,并且仅当价格大于 0 时。

  2. 当产品没有列出价格时,我需要说出价格 "Call for Price",后面没有平方英尺文本。

这是我在网上找到的一个开始,但它们没有任何相关条件,因此它们始终适用于所有产品。

/* 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';
}
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';
 }
}

我希望这能奏效。

谢谢两位的帮助。我使用了你的两个代码字符串,这是我想出的:

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';
 }
}