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.
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.

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.
