# Handling Fee per Item in a Shipping Class

> Charges an extra handling fee for each unit from a shipping class such as "fragile" or "oversized", listed as its own line in the totals.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, shipping, fees, shipping-class
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-shipping-class-handling-fee/

## Code

```php
$wpide_class_fee = array(
    'class'  => 'fragile',               // Shipping class slug.
    'amount' => 2.50,                    // Fee per unit.
    'label'  => 'Fragile item handling', // Line label.
);

add_action( 'woocommerce_cart_calculate_fees', function ( $cart ) use ( $wpide_class_fee ) {
    if ( is_admin() && ! wp_doing_ajax() ) {
        return;
    }

    $units = 0;

    foreach ( $cart->get_cart() as $item ) {
        if ( $item['data']->get_shipping_class() === $wpide_class_fee['class'] ) {
            $units += (int) $item['quantity'];
        }
    }

    if ( $units > 0 ) {
        $cart->add_fee( sprintf( '%s (x%d)', $wpide_class_fee['label'], $units ), $units * $wpide_class_fee['amount'], true );
    }
} );
```

## Notes

Use the shipping class slug from WooCommerce > Settings > Shipping > Classes.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Handling Fee per Item in a Shipping Class" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
