2017-01-31 3 views
0

現在、私はユーザーVATを免除するものに取り組んでいます。私は課税番号(納税者番号)が必要なフィールドを設定しました。私はtrueまたはfalseを返す弁番号validate($vat_number)を検証するphp関数を設定しました。私はバットのフィールドが更新されるたびにPHP/AJAXは、カスタムフィールドに有効な入力がある場合にPHPを実行します。WooCommerce

が、私はこれが本当であるならばvalidate($vat_number) == true

私はWooCommerceから機能set_is_vat_exempt()でVAT免除されるように、現在の顧客を変更したいかどうかを検証したい欲しい

これが成功したら、AJAX update_order_reviewを使用してすべてをVAT免除に設定します。

すべてこれは、ページをリフレッシュ/リロードせずに行う必要があります。

どうすればよいですか?私はphpとjsについてよく知っているが、AJAXはかなり新しくなっている。どんな助けでも大歓迎です。

+0

あなたはこれを理解しましたか?同様の問題に直面してAJAXを使用して税金を取り除こうとしました。 – ryanka

+0

私はやりました。おそらくAJAXの学習を始めるべきです:( –

答えて

0

これを達成するには、AJAXとPHPの組み合わせを使用する必要があります。

JS部分には、次のような検証機能があります。

/* 
* Validates whether or not the necessary fields are provided. 
*/ 
function validateExemptionStatus() { 
    if ($("#wc_checkout_add_ons_3").is(':checked') && $("#wc_checkout_add_ons_4").val() != "" && $("#wc_checkout_add_ons_5").val() != "") { 
     //Check the file upload 
     if ($(".input-file-plupload a.file").attr("href") != "") 
      return true; 
    } 
     return false; 
} 

私の特定のユースケースにはファイルがアップロードされています。あなたは、あなたが検証しようとしているものと一緒にこれを続けたいと思うでしょう。私は追加フィールドを処理するためにWooCommerceアドオンプラグインを使用しています。そのプラグインはupdate_checkoutフックを起動します。フォーカスアウトフィールドから。

私が実際の処理処理する必要がAJAXコード:

function handleTaxExemptionStatus(triggerUpdate) { 
    //Validate whether all fields were filled out or not. 
    if (validateExemptionStatus() === true) { 
     var data_string = "action=example_remove_tax"; 
    } else { 
     var data_string = "action=example_add_tax"; 
    } 
    console.log(data_string); 

    //Make the appropriate AJAX call to add/remove the tax. 
    $.ajax({ 
     url: ajax_url, 
     type:'POST', 
     data: data_string, 
     success: function(html) { 
      if (triggerUpdate) 
       $('body').trigger('update_checkout'); 
     }, 
     error: function(xhr, status, error) { 
      var err = eval("(" + xhr.responseText + ")"); 
      console.log("ERROR:"+err.Message); 
      return false; 
     } 
    }); 


} 

は、今、私たちはクッキンだが。検証する関数と実際のAJAX呼び出しを行う関数があります。今すぐ検証するタイミングを知る必要があります。

/* 
* When the AJAX call is complete, this function validates whether 
* or not the information for the tax exemption was changed. 
* If the information was changed, it makes a call to the handler 
* function to update the information (and the checkout data) accordingly. 
*/ 
$(document).ajaxComplete(function(event, xhr, settings) { 
    //Trigger when the order_review is updated 
    if(settings.url.indexOf('update_order_review') > -1) { 

     var validationStatus = validateExemptionStatus(); 
     var hiddenVarData = $("input#add-remove-tax-trigger").val(); 
     var hiddenVarValue = (hiddenVarData == 'true'); 

     if (hiddenVarValue != validationStatus) { 
      //Set the new value on the hidden field 
      $("input#add-remove-tax-trigger").val(validationStatus); 

      //Update the exemption status 
      handleTaxExemptionStatus(true); 
     } 

    } 
}); 

プラグインは私がフィールドに添付イベントを削除できないだろうので(私はすべてを試してみました) - 私は時に注文状況を更新する際WooCommerceを伝えるために決定するために作成された隠しフィールドに依存しています。それは理想的ではありませんが、機能します。

今 - 仕上げのタッチ。 Wordpressはajax_urlが何であるかを知る必要があります(これを含まなければJSエラーが表示されることがあります)。

/** 
* Adds the WordPress Ajax Library to the frontend. 
*/ 
function add_ajax_library() { 

    $html = '<script type="text/javascript">'; 
     $html .= 'var ajax_url = "' . admin_url('admin-ajax.php') . '"'; 
    $html .= '</script>'; 

    echo $html; 

} // end add_ajax_library 
add_action('wp_head', 'add_ajax_library'); 

あなたのfunctions.phpファイルにこれを追加し、あなたが設定している:私は経由してこれを行います。 WordPressのブートストラップ時に起動します。

今、最後の部分です。 VATの追加/削除を処理するための内部機能が必要です。この部分はシンプルです(ありがたいことに):

function example_remove_tax_from_order(){ 
    global $woocommerce; 
    $woocommerce->customer->set_is_vat_exempt(true); 

    wp_die(); 
} 

add_action('wp_ajax_example_remove_tax', 'example_remove_tax_from_order');   // for logged in user 
add_action('wp_ajax_nopriv_example_remove_tax', 'example_remove_tax_from_order'); // if user not logged in 

function example_add_tax_to_order(){ 
    global $woocommerce; 
    $woocommerce->customer->set_is_vat_exempt(false); 

    wp_die(); 
} 

add_action('wp_ajax_example_add_tax', 'example_add_tax_to_order');   // for logged in user 
add_action('wp_ajax_nopriv_example_add_tax', 'example_add_tax_to_order'); // if user not logged in 

少し複雑ですが、うまく機能します。私は何度も調べてこの問題の解決策を見つけることができませんでした。

関連する問題