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 Handling Fee for Specific Product Categories in WooCommerce?

Do you want to add the extra delivery fees for certain product categories in addition to the available shipping options. This code adds a handling fee to the cart total if any product in the specified category (with the ID ’50’) is present.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_action( 'woocommerce_cart_calculate_fees','ts_custom_category_fee', 20, 1 );
function ts_custom_category_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Set HERE your categories (can be term IDs, slugs or names) in a coma separated array
$categories = array('50');
$fee_amount = 0;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ){
if( has_term( $categories, 'product_cat', $cart_item['product_id']) )
$fee_amount = 60;
}
// Adding the fee
if ( $fee_amount > 0 ){
// Last argument is related to enable tax (true or false)
WC()->cart->add_fee( __( "Handling Fees", "woocommerce" ), $fee_amount, false );
}
}
add_action( 'woocommerce_cart_calculate_fees','ts_custom_category_fee', 20, 1 ); function ts_custom_category_fee( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Set HERE your categories (can be term IDs, slugs or names) in a coma separated array $categories = array('50'); $fee_amount = 0; // Loop through cart items foreach( $cart->get_cart() as $cart_item ){ if( has_term( $categories, 'product_cat', $cart_item['product_id']) ) $fee_amount = 60; } // Adding the fee if ( $fee_amount > 0 ){ // Last argument is related to enable tax (true or false) WC()->cart->add_fee( __( "Handling Fees", "woocommerce" ), $fee_amount, false ); } }
add_action( 'woocommerce_cart_calculate_fees','ts_custom_category_fee', 20, 1 );
function ts_custom_category_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Set HERE your categories (can be term IDs, slugs or names) in a coma separated array
    $categories = array('50');
    $fee_amount = 0;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id']) )
            $fee_amount = 60;
    }

    // Adding the fee
    if ( $fee_amount > 0 ){
        // Last argument is related to enable tax (true or false)
        WC()->cart->add_fee( __( "Handling Fees", "woocommerce" ), $fee_amount, false );
    }
}

Output

When customer chooses any product from the category ‘Electronic’ will be shown an extra delivery or Handling fees as shown below.

How to Add a Handling Fee for Specific Product Categories in WooCommerce? - Tyche Softwares

Same as the above, you can also charge additional fees based on shipping classes. You can add a WooCommerce fees based on shipping class and item quantity that will dynamically increase the additional costs as the quantity increases.

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.