1
votes

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' );
2

2 Answers

5
votes

You can add the follows code snippet -

function refresh_checkout_on_payment_methods_change(){
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="shipping_method"]', function() { 
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}

add_action( 'woocommerce_review_order_before_payment', 'refresh_checkout_on_payment_methods_change' );
0
votes

I managed to solve my problem. The $('body').trigger('update_checkout'); function suggested by itzmekhokan is not working in order to remove the extra fee from the updated order review. I had to simulate the click on one of the payment methods radio buttons to trigger the checkout update.

For the checkout page:

function woocommerce_add_update_checkout() {
    // Only on checkout page
    if( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    (function($){
        $('form.checkout').on( 'change', 'input[name^="shipping_method"]', function() {
        document.getElementById("payment_method_bacs").checked = true;
        });
    })(jQuery);
    </script>
    <?php
}
add_action( 'wp_footer', 'woocommerce_add_update_checkout' );

For the cart page summary:

function reset_payment_method() {
    // Only on cart page
    if( ! is_cart() ) return;
    $payment_method = WC()->session->get( 'chosen_payment_method' );
    if ( $payment_method = 'cod' ) :
        WC()->session->set( 'chosen_payment_method', 'bacs' );
    endif;
}
add_filter( 'woocommerce_before_shipping_calculator', 'reset_payment_method' );