By following these questions and answers (Exclude some products from calculated additional fee in WooCommerce) , (Custom checkbox in WooCommerce admin edit product for a payment fee calculation)
these functions work with line_subtotal , The problem is that when we apply a discount code. Also, the additional percentage is considered from the original full price, not from the price shown by deducting the discount code.
I want the additional percentage to be added only on the price of the products (without shipping costs). In case the discount code is not applied, or applied. Add from the rest of the subtotal price of the products.
I used this
// Percentage tax fee for defined payment methods IDs
add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway' );
function add_checkout_fee_for_gateway( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$targeted_payment_methods = array('paypal', 'bacs', 'cheque'); // Define the payment method(s) ID(s)
// Only on checkout page and for specific payment method ID
if ( is_checkout() && ! is_wc_endpoint_url()
&& in_array(WC()->session->get('chosen_payment_method'), $targeted_payment_methods) ) {
$percentage_rate = 0.09; // Defined percentage rate
$custom_subtotal = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $item ) {
// Get the WC_Product object
$product = wc_get_product( $item['product_id'] );
// Check if the product is excluded from fee calculation
if( $product->get_meta('_tax_fee_excluded') !== 'yes' ) {
// Calculate items subtotal from non-excluded products
$custom_subtotal += $product->get_price() * $item['quantity'];
}
}
if ( $custom_subtotal > 0 ) {
$cart->add_fee( __('9% value added tax'), ($custom_subtotal * $percentage_rate), true, '' );
}
}
}
instead of
// Percentage tax fee for defined payment methods IDs
add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway' );
function add_checkout_fee_for_gateway( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$targeted_payment_methods = array('paypal', 'bacs', 'cheque'); // Define the payment method(s) ID(s)
// Only on checkout page and for specific payment method ID
if ( is_checkout() && ! is_wc_endpoint_url()
&& in_array(WC()->session->get('chosen_payment_method'), $targeted_payment_methods) ) {
$percentage_rate = 0.09; // Defined percentage rate
$custom_subtotal = 0; // Initializing
// Loop through cart items
foreach( $cart->get_cart() as $item ) {
// Get the WC_Product object
$product = wc_get_product( $item['product_id'] );
// Check if the product is excluded from fee calculation
if( $product->get_meta('_tax_fee_excluded') !== 'yes' ) {
// Calculate items subtotal from non excluded products
$custom_subtotal += (float) $item['line_subtotal'];
}
}
if ( $custom_subtotal > 0 ) {
$cart->add_fee( __('9% value added tax'), ($custom_subtotal * $percentage_rate), true, '' );
}
}
}
But I don’t know if this is true or not. In short, my only problem with the code in the second link above is that when the discount code is applied, the surcharge is not calculated correctly. And everything is fine as long as the discount code is not used.