Add Fee In Woocommerce Cart

If you want to apply extra fee in the cart then woocommerce provide an action for applying a fee on the cart.

The fee can be used as shipping fee, installation fee or whatever as per your requirement.

add_action( ‘woocommerce_cart_calculate_fees’, ‘add_extra_cart_fee’ ,10,1 );
This action calculates the total fee and adds into the cart amount.

add_fee();

This woocommerce function is used to add the fee to woocommerce cart.

How to use it ?

<?php
	     add_action( 'woocommerce_cart_calculate_fees', 'add_extra_cart_fee' ,10,1 );
    function add_extra_cart_fee( $cart_object ) {

    global $woocommerce;
    $spfee = 14.00; // initialize special fee
    $woocommerce->cart->add_fee( 'Extra Tax Fee', $spfee, true, 'standard' );

    } 
	
	?>
	

The code is very simple you have to define a global $woocommerce variable and with the help of this variable apply the fee to the cart.

You’ve to specify fee name/label and fee amount, another parameter is optional.

I am just adding 14.00 fee for every product.

if you want to apply the fee for special category or product then you can use $cart_object variable. it contains all the cart products.

using this “foreach ( $cart_object->cart_contents as $key => $value )” you can get the all products by $value[‘product_id’] and can apply your own logic.

For example, if the product Id is 45 then only apply the 23.00 fee or is product category id is 5 then apply 2.00.

you can create own logic and apply the fee.

also, you can specify the fee for each product using a loop.