我有堆栈我尝试自定义代码,但脚本没有将回显值保存到数据库中。
这是我到目前为止所做的事情:
function give_basic_fees_js() {
?>
<script>
<?php //Generate
$digits_needed=3;
global $random_number;
$random_number =''; // set up a blank string
$count=0;
while ( $count < $digits_needed ) {
$random_digit = mt_rand(0, 9);
$random_number .= $random_digit;
$count++;
}?>
var give_global_vars;
//JS for Basic Fee calculation on the fly.
jQuery(function ($) {
/**
* Event Triggers
*/
//When document runs.
$(document).ready(give_basic_update_percentage());
//If donation amount update is triggered.
$(document).on('give_donation_value_updated', function () {
give_basic_update_percentage();
});
//If the donation.
$(document).on('blur', '.give-donation-amount .give-text-input', function (e) {
give_basic_update_percentage();
});
/**
* JS update logic - Basic update percentage JS.
*/
function give_basic_update_percentage() {
//Set vars
var percentage = <?php echo $random_number; ?>;
var current_total = $('input[name="give-amount"]').val();
//Unformat current total so fee calculation is correct for larger donations.
current_total = Math.abs(parseFloat(accounting.unformat(current_total, give_global_vars.decimal_separator)));
var fee = percentage;
var new_total = current_total + fee;
//Set the custom amount input value format properly
var format_args = {
symbol: give_global_vars.currency_sign,
decimal: give_global_vars.decimal_separator,
thousand: give_global_vars.thousands_separator,
precision: give_global_vars.number_decimals
};
fee = accounting.formatMoney(fee, format_args);
new_total = accounting.formatMoney(new_total, format_args);
//Update fee information text.
$('span.give-basic-fee-amount').text(fee);
//Update final total text.
$('.give-final-total-amount').text(new_total);
}
});
</script>
<?php }
add_action( 'wp_print_footer_scripts', 'give_basic_fees_js' );
/**
* Adds the fee to the DB upon submit.
*
* @param $sanitized_amount
*
* @return string
*/
function give_basic_fees_add_fee( $sanitized_amount ) {
//$fee_percentage = (int) give_get_option( 399 );
$fee = $sanitized_amount;
$new_total = $fee + percentage;
return $new_total;
}
add_filter( 'give_donation_total', 'give_basic_fees_add_fee', 1, 1 );
所以我想在DB中记录$ new_total值,公式为费用+ random_number为百分比
由于