Add percentage to the total (including shipping cost) in woocommerce

In Woocommerce I would like to add 3.5% on the Grand Total (including shipping fee). For now im using these code, but this only calculate without shipping fee.

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_donation' );
function woocommerce_custom_donation() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

        $percentage = 0.035; // 3.5%
        // uncomment the line below to include shipping fees in the donation/surcharge calculation
        // $donation = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
    $donation = $woocommerce->cart->cart_contents_total * $percentage;
    $woocommerce->cart->add_fee( 'Administration Fees', $donation, true, '' );
}

Solutions

You can also try this.

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_donation',10, 1 );
function woocommerce_custom_donation() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

        $percentage     = 3.5;
        $cart_total     = $woocommerce->cart->cart_contents_total;
        $shipping_total = $woocommerce->cart->get_shipping_total() ?: 0;
        $tax_total      = $woocommerce->cart->get_taxes_total() ?: 0;

        $grand_total = $cart_total + $shipping_total + $tax_total;
        $donation = $grand_total * $percentage / 100;
        $woocommerce->cart->add_fee( 'Administration Fees', $donation, true, '' );
}

enter image description here

I believe that when the product is in the cart, the delivery method (and related fees) are not known/applied until checkout.

So I would add a "checkout fee" on checkout, and so after the user selected a checkout method (and why not a payment method)

If you enable tax calculation in Woocommerce settings, you could also set up a tax for everyone that also applies on shipping fees:

enter image description here

Similar questions

how to get woocommerce shipping class price/cost using shipping class id
I am trying to get shipping class cost assigned to products. i don't know how to get the cost of the shipping class based on its id can anyone help me?
Set all shipping methods cost to zero for a Free shipping coupon in Woocommerce
I have 3 shipping methods in my cart that should become zero prices as soon as your customer enters Free Shipping coupon. I know how to add a filter in functions.php to detect the coupon but is someone know a snippet to set shipping methods visibles in cart (radio button) to ZERO for this order? My deliveries methods are companies like UPS, FedEx.....
Set shipping cost conditionally based on a shipping class in Woocommerce
We're selling sample products on our Woocommerce site, which is just a variable product. The product has a unique shipping class that allows it to be delivered for 1.99. Actually this cost is always set when if item belongs to that unique shipping class, even if there is other items. I would like if possible to enable that shipping cost only if tha...
Woocommerce set shipping method, get shipping cost
In my functions.php file, I need to be able to set the shipping method (there's only one for this site) and then return the cost for shipping (there are a few costs set). I can see the flat_rate shipping using this: So it's definitely there. Basically, I want the shipping cost returned in from an API call. I have the API routed and all that working...
Shipping cost discount based on a shipping classes in Woocommerce
I'm trying to apply a discount to one shipping class for products currently in a cart. This is applied on the checkout view. In Woocommerce backend, the option is set to charge each shipping class individually. Also, I use only one shipping method named "flat rate". Based on Override all shipping costs for a specific shipping class in Woocommerce, ...
Get shipping class cost using shipping class id in Woocommerce
I need the shipping class cost of products. How can I fetch shipping class cost based on shipping class id?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.