2014-07-22 36 views
0

我目前正在使用此代碼來計算我的某個表單上的稅額,但我想要另一種表單計算稅金,以及我不確定如何編輯此代碼它影響2種形式,或者如果有更好的東西,我可以做?Gravity Forms - 多種形式含稅

 

    // update the "78" to the ID of your form 
    add_filter('gform_pre_render_78', 'add_total_script'); 
    function add_total_script($form) { 
     ?> 

     
     function gform_product_total(formId, total){ 
      var tax = ((5 * total)/100); // update the "20" to the desired tax percentage; 
      tax = Math.round(tax*100)/100; //rounding tax to 2 decimal digits 
      return total + tax; 
     } 
     

     'Tax', // name that will appear in PayPal and pricing summary tables 
      'price' => $tax, // amount of total tax 
      'quantity' => 1 
      ); 

     return $product_info; 
    } 

    function get_total($products) { 

     $total = 0; 
     foreach($products["products"] as $product){ 

      $price = GFCommon::to_number($product["price"]); 
      if(is_array($product["options"])){ 
       foreach($product["options"] as $option){ 
        $price += GFCommon::to_number($option["price"]); 
       } 
      } 
      $subtotal = floatval($product["quantity"]) * $price; 
      $total += $subtotal; 

     } 

     $total += floatval($products["shipping"]["price"]); 

     return $total; 
    } 

回答

0

由於引入重力形式「計算」(不記得是什麼版本)中,如果稅收的計算是非常複雜的,才需要這種方法計算納稅。看起來你的稅很簡單。

下面是展示加稅的比重形式的最佳方法的文章(同時還提供了合併標籤產生的電流小計):

http://gravitywiz.com/subtotal-merge-tag-for-calculations/

確保您check out the demo到看到這在行動。

+0

感謝。我以前看過那篇文章,但我似乎無法讓它爲我工作。它說要在我的functions.php中添加代碼,但是當我這樣做時,我只是得到死亡的白色屏幕,沒有任何工作。這有可能是另一種解決方案嗎?或者我錯過了什麼?如果有幫助,我正在使用名爲Pinboard的免費主題。 – Cameron

+0

查看本頁幫助安裝代碼段: http://gravitywiz.com/documentation/snippet-troubleshooting/ 如果不是,請在文章上留下評論(即使您已經有過),然後我會樂於幫助。 – David

0

請檢查波紋管代碼。 Add javascript to view tax in front end

<script type="text/javascript"> 
    gform.addFilter('gform_product_total', function(total, formId){ 
    var tax = 10, 
     newtax = (total * tax)/100 ; 
     return total + newtax; 
    }); 
</script> 

現在讓我們更新稅收到數據庫。

function saiful_gf_remove_money_symbol($s) { 
    return GFCommon::to_number($s); 
} 
add_filter('saiful_gf_remove_money_symbol', 'saiful_gf_remove_money_symbol'); 

function saiful_gf_to_money($s) { 
    return GFCommon::to_money($s); 
} 
add_filter('saiful_gf_to_money', 'saiful_gf_to_money'); 

add_filter('gform_product_info', 'add_fee', 10, 3); 
function add_fee($product_info, $form, $lead) { 
    $tax = 10; 
    $loop_price = 0; 
    if(isset($product_info['products']) && count($product_info['products']) > 0){ 
     foreach ($product_info['products'] as $data){ 
      if(isset($data['options']) && count($data['options']) > 0){ 
       foreach ($data['options'] as $key => $option) { 
        $loop_price += floatval($option['price']); 
       } 
      } 
      $new_price = apply_filters('saiful_gf_remove_money_symbol',$data['price']) + $loop_price ; 
      $new_price = (($new_price * $tax)/100) + apply_filters('saiful_gf_remove_money_symbol',$data['price']); 
      $data['price'] = apply_filters('saiful_gf_to_money', $new_price); 
      $product_info['products'] = $data; 
     } 
    } 
    return $product_info; 
} 

感謝