これを達成するには、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
少し複雑ですが、うまく機能します。私は何度も調べてこの問題の解決策を見つけることができませんでした。
あなたはこれを理解しましたか?同様の問題に直面してAJAXを使用して税金を取り除こうとしました。 – ryanka
私はやりました。おそらくAJAXの学習を始めるべきです:( –