0
votes

I have tried to implement code found on this site in a number of answers from a particular user with regard to refreshing the checkout based on a change to the payment gateway selected or to some other field change. However, when the JS is included in my function file, my checkout is stuck and I have ajax loading animated circles.

I've already tried to adapt code from:

Trigger ajax update_checkout event on shipping methods change in Woocommerce

Update checkout ajax event when choosing a payment gateway in Woocommerce

On country change ajax update checkout for shipping in Woocommerce

Change Pay button on checkout based on Woocommerce chosen payment method

add_filter( "woocommerce_product_get_tax_class", "woo_diff_rate_for_user", 1, 2 );
add_filter( "woocommerce_product_variation_get_tax_class", "woo_diff_rate_for_user", 1, 2 );
function woo_diff_rate_for_user( $tax_class, $product ) {

// Get the chosen payment gateway (dynamically)
$chosen_payment_method = WC()->session->get('chosen_payment_method');

 if( $chosen_payment_method == 'wdc_woo_credits'){
        $tax_class = "Zero rate";
    } 

<script type="text/javascript">
        (function($){
            $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
                var t = { updateTimer: !1,  dirtyInput: !1,
                    reset_update_checkout_timer: function() {
                        clearTimeout(t.updateTimer)
                    },  trigger_update_checkout: function() {
                        t.reset_update_checkout_timer(), t.dirtyInput = !1,
                        $(document.body).trigger("update_checkout")
                    }
                };
                $(document.body).trigger('update_checkout')
            });
        })(jQuery);
    </script>
     return $tax_class;
}

If I don't include the JS/jQuery, my function, which changes the tax class based on payment option works when the shipping method is changed and the page refreshes on change. But I need the checkout to refresh when the payment gateway is changed not when the shipping is changed.

1

1 Answers

1
votes

You can't include this kind of jQuery script in a filter hook and there is mistakes in your code. Anyways you are not using the right code even for the tax class change.

The replacement code:

add_action( 'woocommerce_before_calculate_totals', 'change_tax_class_based_on_payment_method', 10, 1 );
function change_tax_class_based_on_payment_method( $cart ) {
    // Only for a specific defined payment meyhod
    if ( WC()->session->get('chosen_payment_method') !== 'wdc_woo_credits' )
        return;

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

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        // We set "Zero rate" tax class
        $cart_item['data']->set_tax_class("Zero rate");
    }
}

add_action('wp_footer', 'payment_methods_trigger_update_checkout');
function payment_methods_trigger_update_checkout() {
    if( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
        jQuery(function($){
            $( 'form.checkout' ).on('change', 'input[name="payment_method"]', function() {
                $(document.body).trigger('update_checkout');
            });
        });
    </script>
    <?php
    endif;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

If you use Woo Credits plugin, the correct payment ID is woo_credits, but not wdc_woo_credits.