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

How to Add a Fee Based on Chosen Shipping Method in WooCommerce?

Are you looking to offer your customers more shipping options? This code snippet allows you to automatically add distinct fees based on the specific flat rate shipping method selected by the customer.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_action( 'woocommerce_cart_calculate_fees', 'ts_flat_rate_based_fee', 20, 1 );
function ts_flat_rate_based_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
if( in_array( 'flat_rate:2', $chosen_shipping_methods ) ) {
$fee = array( 'text' => __( "Pickup costs", "woocommerce" ), 'amount' => 12 );
} elseif ( in_array( 'flat_rate:4', $chosen_shipping_methods ) ) {
$fee = array( 'text' => __( "Home delivery costs", "woocommerce" ), 'amount' => 24 );
}
if( isset($fee) ) {
$cart->add_fee( $fee['text'], $fee['amount'], false );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'ts_flat_rate_based_fee', 20, 1 ); function ts_flat_rate_based_fee( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); if( in_array( 'flat_rate:2', $chosen_shipping_methods ) ) { $fee = array( 'text' => __( "Pickup costs", "woocommerce" ), 'amount' => 12 ); } elseif ( in_array( 'flat_rate:4', $chosen_shipping_methods ) ) { $fee = array( 'text' => __( "Home delivery costs", "woocommerce" ), 'amount' => 24 ); } if( isset($fee) ) { $cart->add_fee( $fee['text'], $fee['amount'], false ); } }
add_action( 'woocommerce_cart_calculate_fees', 'ts_flat_rate_based_fee', 20, 1 );
function ts_flat_rate_based_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
      $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );

    if( in_array( 'flat_rate:2', $chosen_shipping_methods ) ) {
        $fee = array( 'text' => __( "Pickup costs", "woocommerce" ), 'amount' => 12 );
    } elseif ( in_array( 'flat_rate:4', $chosen_shipping_methods ) ) {
        $fee = array( 'text' => __( "Home delivery costs", "woocommerce" ), 'amount' => 24 );
    }
    if( isset($fee) ) {
        $cart->add_fee( $fee['text'], $fee['amount'], false );
    }
}

Output

The provided code snippets, add a fee that depends on the chosen shipping method in the cart. Specifically, if the customer selects “Flat Rate 2,” a fee for pickup costs is added. These fees are calculated dynamically and displayed during the checkout process.

How to Add a Fee Based on Chosen Shipping Method in WooCommerce?

Sometimes you might also be required to add fee based on product category and shipping method in WooCommerce which will add extra costs based on certain parameters.

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

Share It:

Subscribe
Notify of


0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
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.