I'm using the Woocommerce Advanced Shipping plugin by Jeroen Sormani for the shipping methods and the WooCommerce Pay for Payment plugin by Karolína Vyskočilová to add a €5 fixed fee to the "cash on delivery" payment method.
Using the Advanced Shipping Plugin, I also created a general rule called "Local pickup" to be always visible to the customers. Then I needed to hide the "cash on delivery" payment method when the "local pickup" shipping option is selected. To achieve this I had to add some code to my functions.php file in order to identify the specific local pickup rule inside the advanced shipping rules:
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
// When 'local pickup' (rule id 21828) has been chosen as shipping rate
if ( in_array( '21828', $chosen_shipping_rates ) ) :
// Unset 'cash on delivery'
unset( $gateways['cod'] );
endif;
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );
Here is my problem: In the checkout page, when I switch to "local pickup", the "cash on delivery" option disappears as said before, but the €5 fee in the order review table stays there. I have to manually switch the payment method again (i.e. from bank transfer to credit card) in order to make the fee disappear.
I need to find some solution to trigger the order review update/refresh when the local pickup is selected.
I tried inserting the following script in the checkout page with no success
function woocommerce_add_update_cart() {
// Only on checkout page
if( ! is_checkout() ) return;
?>
<script type="text/javascript">
jQuery(document.body).trigger("update_checkout");
</script>
<?php
}
add_action( 'wp_footer', 'woocommerce_add_update_cart' );