I would like to update total price on checkout page based on payment method settings.
The problem is that the total isn't updated by the ajax calling. Instead if I refresh the page it is correct.
add_action( 'woocommerce_cart_calculate_fees','payment_method_discount', 20, 1 );
function payment_method_discount( $cart_object ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
foreach($cart_object->cart_contents as $cart_item_id => $cart_item) {
foreach($payment_method_arr as $payment_method) {
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ) {
$cart_item['data']->set_price(99);
}
}
}
I use this filter to trigger it:
add_action( 'woocommerce_review_order_before_payment', 'refresh_payment_methods', 20, 1 );
function refresh_payment_methods() {
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}