我正在使用WordPress的Give插件。他们有一个概念验证插件,允许在结账时添加费用https://github.com/WordImpress/Give-Snippet-Library/blob/master/form-customizations/basic-fees.php
这可以按预期工作,并且没有任何问题,但我想要做的是使费用可选,以便捐赠者可以从前端选择他们是否想要增加该费用。
以下是创建新捐赠amout的代码部分:
function give_basic_fees_add_fee( $sanitized_amount ) {
$fee_percentage = (int) give_get_option( 'basic_fee_percentage' );
$fee = $sanitized_amount * ( $fee_percentage / 100 );
$new_total = $fee + $sanitized_amount;
return $new_total;
}
add_filter( 'give_donation_total', 'give_basic_fees_add_fee', 1, 1 );
我要做的是用Ajax调用它。我尝试了以下,这是行不通的:
JS:
//...
//Use Ajax to see if user checked fee box before submitting
//I've already confirmed this part of the script is loading in the footer.
function check_for_fees(){
var checked;
if($('#gateway-fee').is(':checked')){
checked=true;
}
else {
checked=false;
}
var data = {
'action': 'give_basic_fees_add_fee',
'checked': checked
}
$.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
}
PHP:
add_action('wp_axaj_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
add_action('wp_axaj_nopriv_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
function give_basic_fees_add_fee( $sanitized_amount ) {
/*I just want to alert "Checked" so I know it's working, and create
additional conditional code depending on the value of "checked".*/
$checked = intval($_POST['checked']);
echo $checked;
}
答案 0 :(得分:1)
您的代码中存在拼写错误,这应该是它无法正常工作的原因。
而不是输入
add_action('wp_axaj_give_basic_fees_add_fee', 'give_basic_fees_add_fee'); add_action('wp_axaj_nopriv_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
型:
add_action('wp_ajax_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
add_action('wp_ajax_nopriv_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
ajax调用的默认Wordpress前缀是wp_ajax,而不是wp_axaj。
答案 1 :(得分:0)
嗨试试这可能会对你有帮助。
add_action('wp_axaj_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
add_action('wp_axaj_nopriv_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
function give_basic_fees_add_fee() {
/*I just want to alert "Checked" so I know it's working, and create
additional conditional code depending on the value of "checked".*/
$checked = intval($_POST['checked']);
echo $checked;
//<b>Add exit here otherwise it will give zero along with response.</b>
exit;
}
add_action('wp_axaj_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
add_action('wp_axaj_nopriv_give_basic_fees_add_fee', 'give_basic_fees_add_fee');
function give_basic_fees_add_fee() {
/*I just want to alert "Checked" so I know it's working, and create
additional conditional code depending on the value of "checked".*/
$checked = intval($_POST['checked']);
echo $checked;
//<b>Add exit here otherwise it will give zero along with response.</b>
exit;
}