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

How to Add a WooCommerce Fee Based on Shipping Class and Item Quantity?

This code snippet will help you to assign a shipping cost to the specified shipping class items. Also, it calculates the fee amount based on the total quantity and adds a shipping fee with the text “Big Package Shipping Fee.”

// Add a fee based on shipping class and cart item quantity
add_action( 'woocommerce_cart_calculate_fees', 'ts_shipping_class_and_item_quantity_fee', 10, 1 ); 
function ts_shipping_class_and_item_quantity_fee( $cart ) {

    ## -------------- YOUR SETTINGS BELOW ------------ ##
    $shipping_class = 'heavy-items'; // Targeted Shipping class slug
    $base_fee_rate  = 70; // Base rate for the fee
    ## ----------------------------------------------- ##

    $total_quantity = 0; // Initializing

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        // Get the instance of the WC_Product Object
        $product = $cart_item['data'];

        // Check for product shipping class
        if( $product->get_shipping_class() == $shipping_class ) {
            $total_quantity += $cart_item['quantity']; // Add item quantity

    if ( $total_quantity > 0 ) {
        $fee_text   = __('Big Package Shipping Fee', 'woocommerce');
        $fee_amount = $base_fee_rate * $total_quantity; // Calculate fee amount

        // Add the fee
        $cart->add_fee( $fee_text, $fee_amount );


When a customer adds products with the shipping class ‘heavy-items’ to their cart, the code calculates a special shipping fee based on the total quantity of these items. For each item in the ‘heavy-items’ class, a base fee of $70 is applied. And, if you have, for example, 3 ‘heavy-items’ in your cart, a special shipping fee of $210 will be charged for those items.

How to Add a WooCommerce Fee Based on Shipping Class and Item Quantity?

As the additional charges we have discussed till now in this post are based on product shipping classes and quantity. But you might have some requirements to charge specific category of items too. Look here to know how to add a handling fee for specific product categories in WooCommerce.

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

Share It:

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x