Add a user-optional fee to checkout


I need to add a "packaging" row to my checkout table, which will include two options, like this:
enter image description here

So far, I’ve managed to add a row titled "packaging", with a set fee of 1 € :
enter image description here

Using this code:

add_action( 'woocommerce_cart_calculate_fees','add_packaging_row', 10 , 1 );

function add_packaging_row( $cart ) {
    $cart->add_fee( __('Packaging', 'woocommerce'), (1), true );

And here is the HTML markup for the radio buttons :

        <input type="radio" name="packaging_method" id="basic_packaging" value="basic_packaging" checked="checked">
        <label for="basic_packaging">Basic Packaging <span class="woocommerce-Price-amount amount"><bdi>1&nbsp;<span class="woocommerce-Price-currencySymbol">€</span></bdi></span></label>
        <input type="radio" name="packaging_method" id="amazing_packaging" value="amazing_packaging">
        <label for="amazing_packaging">Amazing Packaging <span class="woocommerce-Price-amount amount"><bdi>2&nbsp;<span class="woocommerce-Price-currencySymbol">€</span></bdi></span></label>

Here are my questions :

  • How do I display the radio buttons as in the screenshot ?
  • How do I recalculate the checkout price based on the packaging option ?
MonsieurPoivron 2 months 0 Answers 13 views 0

Leave an answer