hemnath mouli
hemnath mouli

Reputation: 2755

WooCommerce : Adding fee based on the cart total

I am trying to add fee based on the cart total. But the fee adds on woocommerce_cart_calculate_fees. But I don't know how to make it based on cart total. When the updated or deleted, the total change, so I am unable to get proper total and add fee.

function woo_add_cart_fee() {
    $cart_total = ;
    $fee = (int)$cart_total*5/100;
    WC()->cart->add_fee( 'Credits :', $fee, false, '' );
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

This is my code. I am unable to find the way. Can anyone help.? Thanks in advance.

Upvotes: 1

Views: 1486

Answers (1)

Døner M.
Døner M.

Reputation: 353

Try this:

function woo_dm_add_custom_fees(){
    $cart_total = 0;
    foreach( WC()->cart->get_cart() as $item ){ 
        $cart_total += $item["line_total"];
    }
    $fee = (int)$cart_total*5/100;
    WC()->cart->add_fee( "Credits :", $fee, false, '' );
}

add_action( 'woocommerce_cart_calculate_fees' , 'woo_dm_add_custom_fees' );
add_action( 'woocommerce_after_cart_item_quantity_update', 'woo_dm_add_custom_fees' );

Upvotes: 2

Related Questions