我使用的是用於WordPress的Give插件。他們有一個概念驗證插件,允許在結帳時添加費用https://github.com/WordImpress/Give-Snippet-Library/blob/master/form-customizations/basic-fees.php使用Ajax調用WordPress過濾器動作
這種方式按預期方式工作,並沒有問題,但我想要做的是使費用可選,以便捐助者可以選擇前端是否要增加這筆費用。
下面是創建新的捐贈大寫金額的部分代碼:
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;
}
就是這樣!我昨晚注意到這一點,忘記了更新問題。 – user3183717