Refreshing WooCommerce Checkout totals using AJAX

Hi there, I have a custom checkbox on the WooCommerce checkout page. When it is checked, I want to make an AJAX call to re-calculate the cart totals on the checkout page. I’ve got the following JavaScript that is triggered when the checkbox is checked:

============================ START JS AJAX Script ============================================
<script type=“text/javascript”>
jQuery(document).ready(function(){
jQuery(’#myfield1_checkbox’).change(
function(){
//alert(jQuery(this).val());
if (jQuery(this).is(’:checked’)) {
var data = {
action: ‘woocommerce_update_order_review’,
security: wc_checkout_params.update_order_review_nonce,
myfield1: 1,
post_data: jQuery( ‘form.checkout’ ).serialize()
};
jQuery.ajax({
type: ‘POST’,
url: wc_checkout_params.ajax_url,
data: data,
success: function (code) {
console.log(code);
var order_output = jQuery(code);
jQuery( ‘#order_review’ ).html( response[‘fragments’][’.woocommerce-checkout-review-order-table’]+response[‘fragments’][’.woocommerce-checkout-payment’]);

				jQuery('body').trigger('updated_checkout');
			    },
			    dataType: 'html'
			});
		} 
	    });

});

</script>
============================ END JS AJAX Script ============================================

I then have the corresponding action in functions.php to do the calculation:

====================================== ACTION START===============================================
add_action(‘woocommerce_update_order_review’, ‘my_woocommerce_update_order_review’, 10);
function my_woocommerce_update_order_review($post_data) {

$surcharge = 25;
global $woocommerce;
parse_str( $post_data, $post_data );

if ( ‘1’ == $post_data[‘myfield1’] ) {

if ( is_admin() &amp;&amp; ! defined( 'DOING_AJAX' ) )
	return;

$woocommerce-&gt;cart-&gt;add_fee( 'Surcharge', $surcharge, true, '' );

}

}

====================================== ACTION END ===============================================

The cart if refreshing - because I can see the AJAX response in my browser console, however, the action is never called, as the cart totals remains the same.

Questions:

  1. Is this a best practice way of adding a surcharge to the cart totals?
  2. If yes, then what am I doing wrong? I have replaced the action code to do something simple too and no luck.
  3. Is woocommerce_update_order_review the correct hook that I should be calling?

Thanks

Hi,

We can’t help you much with custom code, especially as it is for a plugin. It’s best to seek help on Woocommerce forums for this, just to make sure you get it right.

Regarding your questions:

  1. Can’t tell you that.
  2. Your action in your AJAX code should be the actual function name, not the action hook.
  3. I don’t know how it is for WC, but in general when you’re doing this in WordPress you need to hook your callbacks like this:

add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );

So hooking into woocommerce_update_order_review probably achieves nothing.
More info on this here: https://codex.wordpress.org/AJAX_in_Plugins