现在,我正在做的事情,将使用户增值税豁免。我已经设置了一个需要税号(增值税号)的字段。我已经设置了一个php函数来验证vat validate($vat_number),该函数返回true或false。
我想要什么
每当vat字段更新时,我都想验证是否是validate($vat_number) == true
如果这是真的,我希望将当前客户更改为不使用函数set_is_vat_exempt()而不受WooCommerce限制的vat。
成功后,我希望使用AJAX update_order_review将所有内容设置为增值税豁免。
所有这些都应该在不刷新/重新加载页面的情况下进行。
我该怎么办?我对php和js有很好的了解,但是AJAX对我来说还是很新的。任何帮助都将不胜感激。
发布于 2017-10-13 15:32:34
您必须使用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添加-Ons插件来处理其他字段。当您从字段中聚焦时,该插件会触发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引导时,它将触发。
现在最后一块。我们需要一个内部功能来处理增值税的加/移。这个部分很简单(谢天谢地):
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有点复杂,但效果很好。我到处找,找不到解决这个问题的办法。
https://stackoverflow.com/questions/41961744
复制相似问题