WooCommerce dynamic retention based on checkout switch

How do I get a radio button group to update the content specified in the statement total on the WooCommerce Checkout page when it is selected and then another. Choosing Radio Button A should add 0.05 percent as an extra charge, as Radio Button B would add 0.10 percent.

I know I can add static overhead with the following:

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;

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

// $percentage is what will change based on which button is selected A or B
// however this is not executed after the page is loaded
$percentage = 0.05;

$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
$woocommerce->cart->add_fee( 'Service Fee', $surcharge, true, 'standard' );

}

      

+3


source to share


2 answers


So I solved it by getting AJAX, edited the button change and called a PHP callback that sets the WC-> session-> set variable. Then a location.reload is emitted in AJAX, which forcibly reloads the functions.php. Inside my co-pay filter, I then checked for a session variable and added a charge or not based on the session value.



+1


source


Maybe something like this will help you get started. I don't think it will add the board automatically, but I haven't tested it and this is just an idea:

jQuery(document).ready(function($){

    if ( typeof wc_checkout_form === 'undefined' || ) {
        return false;
    }

    $checkout_form = $( 'form.checkout' );
    $checkout_form.on( 'change', '.your_radio_buttons_class', function(){
        wc_checkout_form.wc_checkout_form();
    });

});

      



I believe you will need to use a checkout script , which is why you want your custom script to be loaded after checkout.js

.

0


source







All Articles