How To add an additional percentage amount in between specific total cart values Woocommerce

If you want to charge extra amount with woocommerce cart total, action hooks named ‘woocommerce_cart_calculate_fees’ helps you to achieve your goal. There are two types of hooks available in WordPress – one is action hooks and another is filter hooks. woocommerce_cart_calculate_fees’ is an action hook. Please check the code and image below below.

add_action( 'woocommerce_cart_calculate_fees', 'sujoyroy_add_amount_on_cart_total', 10, 1 );
function sujoyroy_add_amount_on_cart_total( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Additional Amount
$tax_per = get_field('hst_tax', 'option') ? get_field('hst_tax', 'option') : 0;
// The cart sub total
$cart_total = $cart->cart_contents_total; ; 
$extra_amt = ($cart_total*$tax_per)/100;
if ( $extra_amt != 0 ) 
$cart->add_fee( __( "Hst Tax", "woocommerce" ), $extra_amt, false );
}

In cart page Extra amount of tax have added with subtotal to create total amount.
In above code I have built a function name with ‘sujoyroy_add_amount_on_cart_total’ and pass the ‘$cart’ variable – It will helps to get all cart data in woocommerce. Aftermath I have used the a variable ‘$tax_per’ which is nothing but a fetch the percentage amount from wordpress backend and have php Ternary Operator– It is nothing but a small code of if else condition in php – Fetch the cart total amount using this ‘$cart->cart_contents_total’ and assign it to a variable in $cart_total and calculate extra amount in percentage formula – add it to add fee data. In that way we can add any extra amount in cart page

Leave a Message

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
By commenting you accept the Privacy Policy