Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Calculate Dynamic Fees from Custom Checkout Fields in WooCommerce?

WooCommerce provides a flexible checkout process, but sometimes stores need extra features to offer more options and services to customers. One such feature is adding fees dynamically based on user selection, applied only when certain conditions, like a minimum subtotal, are met.

For example, a store could allow customers to add gift wrapping via a checkbox. When the customer selects this option and the cart subtotal exceeds a defined amount (e.g., $50), a $5 fee is added automatically.

Solution: Calculate Dynamic Fees from Custom Checkout Fields

Here’s a simple code snippet that lets you add a gift wrapping checkbox option during checkout. When selected, a $5 fee is applied automatically if the cart subtotal is greater than $50.

Note: This code snippet has been tested and works on WordPress 6.8.2 and WooCommerce 10.1.2.
add_action( 'woocommerce_review_order_before_payment', 'ts_add_gift_wrap_checkbox' );
function ts_add_gift_wrap_checkbox() {
    woocommerce_form_field( 'ts_gift_wrap', array(
        'type'  => 'checkbox',
        'class' => array( 'form-row-wide' ),
        'label' => __( 'Add Gift Wrapping for $5 (applies if subtotal > $50)' ),
    ));
}

// Save checkbox value into session
add_action( 'woocommerce_checkout_update_order_review', 'ts_save_gift_wrap_session' );
function ts_save_gift_wrap_session( $post_data ) {
    parse_str( $post_data, $data );
    WC()->session->set( 'ts_gift_wrap', ! empty( $data['ts_gift_wrap'] ) );
}

// Apply fee conditionally
add_action( 'woocommerce_cart_calculate_fees', 'ts_add_gift_wrap_fee' );
function ts_add_gift_wrap_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $gift_wrap = WC()->session->get( 'ts_gift_wrap' );
    $subtotal  = $cart->get_subtotal();

    if ( $gift_wrap && $subtotal > 50 ) {
        $cart->add_fee( __( 'Gift Wrapping', 'woocommerce' ), 5 );
    }
}

// Save field to order meta 
add_action( 'woocommerce_checkout_create_order', 'ts_save_gift_wrap_order_meta', 20, 2 );
function ts_save_gift_wrap_order_meta( $order, $data ) {
    $gift_wrap = WC()->session->get( 'ts_gift_wrap' );
    $order->update_meta_data( '_ts_gift_wrap', $gift_wrap ? 'Yes' : 'No' );
}


// Show value in admin order details page (below billing address)
add_action( 'woocommerce_admin_order_data_after_billing_address', 'ts_display_gift_wrap_admin_order_meta' );
function ts_display_gift_wrap_admin_order_meta( $order ) {
    $gift_wrap = get_post_meta( $order->get_id(), '_ts_gift_wrap', true );
    echo '<p><strong>' . __( 'Gift Wrapping:' ) . '</strong> ' . ( $gift_wrap ?: 'No' ) . '</p>';
}

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

Output

A custom checkbox option is displayed on the checkout page. Customers can choose gift wrapping via this checkbox, and if the cart subtotal is greater than $50, a $5 fee is added to the order. This way, the fee only applies when both conditions are met.

Custom Checkout Fields

The selection is stored in the order meta, so you can see whether the customer opted for gift wrapping directly from the admin order page.

Dynamic fees like this make your checkout more interactive and flexible, allowing you to charge for optional services without complicating the shopping experience. You can easily charge for optional services, like special packaging, only when a customer chooses them.

Another useful feature is adding custom fields dynamically to the checkout page, such as showing extra fields when the cart quantity crosses a certain limit. These options give you the flexibility to customize checkout based on what works best for your store.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.