cmadurawala
cmadurawala

Reputation: 31

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, '' );
}

Upvotes: 3

Views: 739

Answers (2)

Rajeev Singh
Rajeev Singh

Reputation: 1809

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

Upvotes: 3

Mtxz
Mtxz

Reputation: 3869

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

Upvotes: 1

Related Questions