在Woocommerce结帐页面使用AJAX和Fee API进行动态折扣
16lz
2021-01-22
I have put a select box in the checkout page like this.
我在结帐页面中放了一个选择框,就像这样。
function rx_wc_reward_points_check() {
$reward_points = '<select class="rx-rewad-points" id="rx-redemption-points">
<option value="1250">$25.00 Off (1250 Points) </option>
<option value="2500">$50.00 Off (2500 Points) </option>
<option value="5000">$100.00 Off (5000 Points) </option>
<option value="7000">$150.00 Off (7000 Points) </option>
</select>';
$reward_points .= '<a class="button alt" name="rx_reward_points_btn" id="rx_reward_points" value="Apply" data-value="Reward Points">Apply Now</a>';
echo $reward_points;
}
add_action( 'woocommerce_checkout_after_customer_details', 'rx_wc_reward_points_check', 10, 0 );
After that i have added this to the function file for
之后我将其添加到函数文件中
function rx_wc_deduct_reward() {
global $woocommerce;
$reward_value = $_POST['rewardpoints'];
WC()->cart->add_fee( 'Fee', -$reward_value );
echo 'success';
exit;
}
add_action( 'wp_ajax_rx_wc_deduct_reward', 'rx_wc_deduct_reward' );
add_action( 'wp_ajax_nopriv_rx_wc_deduct_reward', 'rx_wc_deduct_reward' );
This is using for the ajax part
这用于ajax部分
jQuery(document).ready( function() {
jQuery('#rx_reward_points').on('click', function() {
var selectedrewad = jQuery( "#rx-redemption-points option:selected" ).val();
jQuery.ajax({
type : "post",
url : ajax_var.ajaxurl,
data : { action: "rx_wc_deduct_reward", rewardpoints : selectedrewad },
success: function(response) {
console.log(response)
if(response == "success") {
console.log('done');
}
else {
console.log("Your vote could not be added")
}
}
});
})
})
But it is not working. I have done some mistake in the "rx_wc_deduct_reward" function. I cant figure out how to do this.
但它没有用。我在“rx_wc_deduct_reward”函数中做了一些错误。我无法弄清楚如何做到这一点。
Any help is appreciated.
任何帮助表示赞赏。
1 个解决方案
#1
2
You need to make it slight different to get it work as you get some errors and missing parts:
当你遇到一些错误和遗漏部分时,你需要让它略有不同才能使它工作:
// Displaying a select field and a submit button in checkout page
add_action( 'woocommerce_checkout_after_customer_details', 'rx_wc_reward_points_check', 10, 0 );
function rx_wc_reward_points_check() {
echo '<select class="rx-rewad-points" id="rx-redemption-points">
<option value="25">' . __("$25.00 Off (1250 Points)", "woocommerce" ) . '</option>
<option value="50">' . __("$50.00 Off (2500 Points)", "woocommerce" ) . '</option>
<option value="100">' . __("$100.00 Off (5000 Points)", "woocommerce" ) . '</option>
<option value="150">' . __("$150.00 Off (7000 Points)", "woocommerce" ) . '</option>
</select>
<a class="button alt" name="rx_reward_points_btn" id="rx_reward_points" value="Apply" data-value="Reward Points">Apply Now</a>';
}
// jQuery - Ajax script
add_action( 'wp_footer', 'rx_wc_reward_points_script' );
function rx_wc_reward_points_script() {
// Only checkout page
if ( ! is_checkout() ) return;
?>
<script type="text/javascript">
jQuery( function($){
$('#rx_reward_points').on('click', function() {
$.ajax({
type: "post",
url: wc_checkout_params.ajax_url,
data: {
'action' : 'rx_wc_deduct_reward',
'rewardpoints' : $("#rx-redemption-points").val()
},
success: function(response) {
$('body').trigger('update_checkout');
console.log('response: '+response); // just for testing | TO BE REMOVED
},
error: function(error){
console.log('error: '+error); // just for testing | TO BE REMOVED
}
});
})
})
</script>
<?php
}
// Wordpress Ajax code (set ajax data in Woocommerce session)
add_action( 'wp_ajax_rx_wc_deduct_reward', 'rx_wc_deduct_reward' );
add_action( 'wp_ajax_nopriv_rx_wc_deduct_reward', 'rx_wc_deduct_reward' );
function rx_wc_deduct_reward() {
if( isset($_POST['rewardpoints']) ){
WC()->session->set( 'custom_fee', esc_attr( $_POST['rewardpoints'] ) );
echo true;
}
exit();
}
// Add a custom dynamic discount based on reward points
add_action( 'woocommerce_cart_calculate_fees', 'rx_rewardpoints_discount', 20, 1 );
function rx_rewardpoints_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for targeted shipping method
if ( WC()->session->__isset( 'custom_fee' ) )
$discount = (float) WC()->session->get( 'custom_fee' );
if( isset($discount) && $discount > 0 )
$cart->add_fee( __( 'Reward discount', 'woocommerce' ), -$discount );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作
更多相关文章
- Jquery中的队列函数quene()、dequene()、clearQuene()
- Internet Explorer导致无效的真实性令牌错误
- DataTables警告:table id = DataTables_Table_0 - Ajax错误。有
- 如何在使用jquery验证和自定义错误放置时清除错误
- Ajax调用php脚本返回404错误
- Ajax_04之jQuery中封装的Ajax函数
- Jquery验证插件,获取错误字符串
- 怎么用js或jquery把一个函数b绑定到另一个函数a之后执行
- 与symfony一起使用ajax时出现500内部服务器错误