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.
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.
![](https://www.tychesoftwares.com/wp-content/uploads/2023/12/custom-fees-based-on-product-category-1024x741.png)
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.