The trend of adding charges or discounts for different payment methods on the checkout page has become increasingly common in WooCommerce stores. It is because each payment gateway charges a transaction processing fee to store owners.
For example, if credit cards have low transaction processing fees you can provide discounts to customers who choose the credit card payment method. On the other hand, if payment methods like Cash on Delivery carry some risks, then you can charge a high transaction processing fee and you can add that extra charge to customers who choose Cash on Delivery.
This post will help you to add a custom fee to a WooCommerce payment method based on the order total threshold and thus it will allow you to recover some costs.
Where to Add Custom Code in WooCommerce?
It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.
Preliminary steps
You need to decide two things before you start implementing the below code.
- Make sure you choose the payment method to which you will assign the custom fee when the order total is below a certain threshold.
- Determine the order total threshold.
Solution: WooCommerce Checkout fees based on payment method & order total threshold
Imagine you own an apparel store that has a huge collection of apparel and accessories. As a store owner, you might find it necessary to configure custom fees for small orders, for a particular payment gateway, and based on the order total threshold.
Here is an example use case: The code snippet applies a $5 custom fee for orders below $100 when customers choose the “Bank Transfer” (BACS) payment method.
// Enable BACS for orders below a certain total and add a fee to BACS add_action('woocommerce_cart_calculate_fees', 'ts_add_checkout_fee_for_bacs'); function ts_add_checkout_fee_for_bacs() { if (!is_checkout()) { return; } $chosen_gateway = WC()->session->get('chosen_payment_method'); if ($chosen_gateway === 'bacs') { $order_total = WC()->cart->subtotal; $fee = $order_total < 100 ? 5 : 0; // Remove the fee if the order total is greater than 100 if ($order_total > 100) { foreach (WC()->cart->get_fees() as $fee_key => $fee) { if ($fee->name === 'Custom Fee') { WC()->cart->remove_fee($fee_key); break; } } } else { WC()->cart->add_fee('Custom Fee', $fee); } } } // Refresh checkout on payment method change add_action('woocommerce_after_checkout_form', 'ts_refresh_checkout_on_payment_methods_change'); function ts_refresh_checkout_on_payment_methods_change() { wc_enqueue_js(" $('form.checkout').on('change', 'input[name^=\'payment_method\']', function() { $('body').trigger('update_checkout'); }); "); }
Output
In the below output, the customer has chosen the “Bank Transfer” (BACS) payment method, and the Order subtotal is less than $100. As per our conditioning, the custom fee of $5 will be added to the total.
In another scenario, if the customer chooses a different payment method other than the Direct bank transfer, the extra custom fee of $5 will not be added.
Understanding the code snippet in detail:
- The code starts by using the ts_add_checkout_fee_for_bacs() function to hook into the woocommerce_cart_calculate_fees action, which is called when WooCommerce calculates fees for the cart.
- It first checks if the current page is the checkout page using is_checkout().
- It then gets the chosen payment method from the session using WC()->session->get(‘chosen_payment_method’)
- if($chosen_gateway === ‘bacs’) is a conditional statement that checks if the chosen payment is equal to ‘bacs’. It proceeds to calculate and add the custom fee to the cart based on the order total. If the order total is less than 100, it sets the fee to 5, otherwise, it sets the fee to 0.
- $order_total is used to hold the cart’s total value.
- If the order total is greater than 100, the code checks for an existing fee named ‘Custom Fee’ in the cart using WC()->cart->get_fees().
- If the fee exists, it removes it from the cart using WC()->cart->remove_fee($fee_key), where $fee_key is the key of the fee to be removed.
- The ts_refresh_checkout_on_payment_methods_change() function is hooked to the woocommerce_after_checkout_form action. This part of the code is responsible for refreshing the checkout page whenever there is a change in the selected payment methods. By doing so, it ensures that any fee updates are recalculated and displayed correctly to the customer during the checkout process.
Conclusion
The above code snippet demonstrates how WooCommerce store owners can efficiently manage custom fees for the Bank Transfer (BACS) payment method. You can modify the code for other payment methods as well as per your requirement and define the order total threshold.
We hope you will find this article useful. Let us know how the code snippet works for you in the comment section below.