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

How to Add Fee Based on Product Category and Shipping Method in WooCommerce?

By adding fees based on certain parameters, store owners can better reflect real-world costs associated with certain products and shipping methods. The below code snippet can help to add fees based on product category and chosen shipping method in WooCommerce.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_action( 'woocommerce_cart_calculate_fees','ts_custom_category_fee');
function ts_custom_category_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
// 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 = 35;
}
// Only for Local pickup chosen shipping method
if ( strpos( $chosen_shipping_method_id, 'free_shipping' ) !== false || strpos( $chosen_shipping_method_id, 'filters_by_cities_shipping_method' ) !== false ) {
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// Quantity
$product_quantity = $cart_item['quantity'];
// Check for category
if ( has_term( $special_fee_cat, 'product_cat', $product_id ) ) {
$total += $fee_per_prod * $product_quantity;
}
}
// Adding the fee
if ( $fee_amount > 0 ){
// Last argument is related to enable tax (true or false)
WC()->cart->add_fee( __( "Additional Fees", "woocommerce" ), $fee_amount, false );
}
}
}
add_action( 'woocommerce_cart_calculate_fees','ts_custom_category_fee'); function ts_custom_category_fee( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0]; $chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0]; // 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 = 35; } // Only for Local pickup chosen shipping method if ( strpos( $chosen_shipping_method_id, 'free_shipping' ) !== false || strpos( $chosen_shipping_method_id, 'filters_by_cities_shipping_method' ) !== false ) { // Loop though each cart items and set prices in an array foreach ( $cart->get_cart() as $cart_item ) { // Get product id $product_id = $cart_item['product_id']; // Quantity $product_quantity = $cart_item['quantity']; // Check for category if ( has_term( $special_fee_cat, 'product_cat', $product_id ) ) { $total += $fee_per_prod * $product_quantity; } } // Adding the fee if ( $fee_amount > 0 ){ // Last argument is related to enable tax (true or false) WC()->cart->add_fee( __( "Additional Fees", "woocommerce" ), $fee_amount, false ); } } }
add_action( 'woocommerce_cart_calculate_fees','ts_custom_category_fee');
function ts_custom_category_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];

    // 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 = 35;
    }
    
    // Only for Local pickup chosen shipping method
    if ( strpos( $chosen_shipping_method_id, 'free_shipping' ) !== false || strpos( $chosen_shipping_method_id, 'filters_by_cities_shipping_method' ) !== false ) {
        // Loop though each cart items and set prices in an array
        foreach ( $cart->get_cart() as $cart_item ) {
            // Get product id
            $product_id = $cart_item['product_id'];

            // Quantity
            $product_quantity = $cart_item['quantity'];

            // Check for category
            if ( has_term( $special_fee_cat, 'product_cat', $product_id ) ) {
                $total += $fee_per_prod * $product_quantity;
            }
        }

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

Output

When products from the ‘Electronic’ category are added to the cart and if the chosen shipping method is ‘Free shipping’ an extra fee of $35 is added to the cart totals.

How to Add Fee Based on Product Category and Shipping Method in WooCommerce? - Tyche Softwares

Alternatively, you can also add WooCommerce Checkout fees based on shipping method and payment method.

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.