php - WooCommerce: Set shipping method to free by selected payment method
one text
I am trying to set the shipping method to free when the client has selected a specific payment method. Currently, my approach is the following:
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
function custom_handling_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_payment_id = WC()->session->get('chosen_payment_method');
if (WC()->customer->get_billing_country() == 'IT' )
{
if($chosen_payment_id === "visa" || $chosen_payment_id === "bacs")
{
WC()->session->set('chosen_shipping_methods', array( 'free_shipping' ) );
$cart->set_shipping_total(0);
}
}
}
add_action( 'wp_footer', 'custom_checkout_jquery_script' );
function custom_checkout_jquery_script() {
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery( function($){
jQuery('form.checkout').on('change', 'input[name="payment_method"]', function(){
jQuery(document.body).trigger('update_checkout');
});
});
</script>
<?php
endif;
}
With the above code, the shipping method which as previously selected, and let's assume it had 2?�� fee, is now disappeared from the fees, but it is still calculated. How can I update that?
Source