0

I'm trying to add a few radio options to my checkout page.

When a user selects a radio option, I want to automatically refresh the page and add a fee to the checkout page based on what was selected.

For example:

If you select "Option 1" it will add a fee of $10.

If you select "Option 2" it will add a fee of $20 etc.

Here's my code in the functions.php file:

add_action( 'woocommerce_cart_calculate_fees','calculate_extra_deliveryoptions' );

function calculate_extra_deliveryoptions() {

   echo "<form action=\"\"><br/>";
   echo "<input type=\"radio\" name=\"deliveryextras\" id=\"option1\" value=\"10\">Option 1 <br/>";
   echo "<input type=\"radio\" name=\"deliveryextras\" id=\"option2\" value=\"20\">Option 2 <br/>";
   echo "</form>";
    
   WC()->cart->add_fee(__('A small fee', 'option'), 5);
    
}

I can't seem to figure out how to get the selected value, refresh the page and add the fee.

Khoa
  • 261
  • 1
  • 2
  • 19
  • 1
    [WooCommerce: Add Checkout Fees Based on Custom Radio Button](https://www.businessbloomer.com/woocommerce-add-checkout-fees-based-on-radio-button-choice-ajax/) – 7uc1f3r Sep 24 '20 at 06:44
  • Thanks I tried that and it displays the options but when I click on it, it doesn’t refresh the page? – Khoa Sep 24 '20 at 08:58
  • 1
    It is done via an ajax call, why would the **entire page** have to be refreshed? – 7uc1f3r Sep 24 '20 at 08:58
  • I meant that the Ajax call didn’t refresh. I copied the code and pasted into functions.php. Not sure if I have to do anything else on the client side for Ajax? – Khoa Sep 24 '20 at 09:02
  • 1
    I have not written that code but I can confirm that it works without more – 7uc1f3r Sep 24 '20 at 09:05
  • 1
    Ok good to know. I’ll try again and see if something is blocking the Ajax call – Khoa Sep 24 '20 at 09:08

0 Answers0