Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/AJAX在自定义字段具有有效输入时执行PHP_Php_Ajax_Wordpress_Woocommerce - Fatal编程技术网

PHP/AJAX在自定义字段具有有效输入时执行PHP

PHP/AJAX在自定义字段具有有效输入时执行PHP,php,ajax,wordpress,woocommerce,Php,Ajax,Wordpress,Woocommerce,现在我正在做一件可以让用户免缴增值税的事情。我已经设置了一个需要税号(vat号码)的字段。我已经设置了一个php函数来验证增值税编号validate($vat\u number),该函数返回true或false 我想要什么 无论何时更新增值税字段,我都希望验证是否validate($vat\u number)==true 如果这是真的,我想使用WooCommerce提供的函数set\u is\u vat\u免税()将当前客户更改为vat免税客户 成功后,我想使用AJAXupdate\u orde

现在我正在做一件可以让用户免缴增值税的事情。我已经设置了一个需要税号(vat号码)的字段。我已经设置了一个php函数来验证增值税编号
validate($vat\u number)
,该函数返回true或false

我想要什么

无论何时更新增值税字段,我都希望验证是否
validate($vat\u number)==true

如果这是真的,我想使用WooCommerce提供的函数
set\u is\u vat\u免税()
将当前客户更改为vat免税客户

成功后,我想使用AJAX
update\u order\u review
将所有内容设置为VAT免税

所有这些都应该在不刷新/重新加载页面的情况下发生


我该怎么办?我对php和js有很好的了解,但AJAX对我来说是相当陌生的。任何帮助都将不胜感激

您必须结合使用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\uURL(如果不包含此内容,您可能会看到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' );

有点复杂,但效果很好。我到处找,找不到解决这个问题的办法。

你找到了吗?在尝试使用AJAX删除税收时也面临着类似的问题。但愿我能做到。。我可能应该开始学习AJAX:(
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