phpwordpresswoocommercetaxonomy-termsfee

Add a fee for specific product categories in WooCommerce


I have a client who has a WordPress + WooCommerce store. He asked me to include a fee in the store, but that not all categories would be charged. I would like to insert a fee of 7.40 (Receipt Notice Fee) for all product categories, but excluding some categories.

I tried the code below:

add_action('woocommerce_cart_calculate_fees', 'add_fees_on_ids');

function add_fees_on_ids() {
    if (is_admin() && !defined('DOING_AJAX')) {return;}
    
    foreach( WC()->cart->get_cart() as $item_keys => $item ) {
        if( in_array( $item['product_cat'], fee_ids() )) {
            WC()->cart->add_fee(__('Additional Charge'), 5);
        }
    }
}

function fee_ids() {
    return array( 600,650,700 );
}

Solution

  • There are some mistakes in your code. To target specific product categories, use WordPress has_term() conditional function.

    Try one of the following instead:

    1). Cumulated fee for specific product categories:

    add_action( 'woocommerce_cart_calculate_fees', 'add_custom_category_fee', 10, 1 );
    function add_custom_category_fee( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
            return;
        }
    
        $targeted_term_ids = array( 600, 650, 700 ); // Targeted category(ies) term ID(s)
        $fee_by_item = 5; // Define the fee amount by item
        $fee_total = 0; // Initialize
        
        // Loop through cart items
        foreach( $cart->get_cart() as $item ) {
            // Increase the total fee amount only for the targeted categories
            if ( has_term( $targeted_term_ids, 'product_cat', $item['product_id'] ) ) {
                $fee_total += $fee_by_item;
            }
        }
    
        // Add the Fee
        if ( $fee_total > 0 ) {
            $cart->add_fee( __('Additional Charge', 'woocommerce'), $fee_total );
        }
    }
    

    You can also handle the product quantity by replacing in the code:

    $fee_total += $fee_by_item;
    

    with:

    $fee_total += $fee_by_item * $item['quantity'];
    

    2). Add a unique fee if specific product category(ies) is(are) in cart:

    add_action( 'woocommerce_cart_calculate_fees', 'add_custom_category_fee', 10, 1 );
    function add_custom_category_fee( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
            return;
        }
    
        $targeted_term_ids = array( 600, 650, 700 ); // Targeted category(ies) term ID(s)
        $fee_amount = 5; // Define the fee amount
        $category_found = false; // Initialize
        
        // Loop through cart items
        foreach( $cart->get_cart() as $item ) {
            // Check for the targeted product categories
            if ( has_term( $targeted_term_ids, 'product_cat', $item['product_id'] ) ) {
                $category_found = true; // Category found
            }
        }
    
        // Add the Fee
        if ( $category_found ) {
            $cart->add_fee( __('Additional Charge', 'woocommerce'), $fee_amount );
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.