代码之家  ›  专栏  ›  技术社区  ›  Luke Baumann

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

  •  2
  • Luke Baumann  · 技术社区  · 8 年前
    1. 我有一些产品的价格都需要以“每平方英尺”结束。我希望这只适用于特定类别的产品,并且只有当价格大于0时。

    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';
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Niket Joshi    8 年前
    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';
     }
    }
    

    我希望这能奏效。

        2
  •  0
  •   LoicTheAztec    8 年前

    谢谢你们俩的帮助。我使用了您的两个代码字符串,下面是我得出的结论:

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