Are you searching for an easy way to add extra fees to the checkout in your WooCommerce store?
In today’s tutorial, we will show you how to add fees to the WooCommerce checkout programmatically to help you maximize your revenue.
There are times when you may need customers to pay an additional charge during the checkout.
The good news is that WooCommerce provides you with a lot of flexibility to customize and add the charges to the order cycle.
When to Add Fees to the WooCommerce Checkout?
Here are some of the common situations where you need to add fees during checkout:
- Handling Fees – This is an additional charge you include when you have to deliver products that are considered fragile. These are products that can be easily damaged and need to be transported with extra care and special packaging.
- Payment Gateways – You might want to transfer transactional charges to your customers depending on the payment methods you accept in your store.
- Taxable Amount – If you do not want to include the tax in the product price, you can add it as an additional charge on the checkout.
It is worth mentioning that using code snippets to customize your store is not always easy.
However, it gives you much control over the customization you make compared to plugins.
This means that this solution is reserved for those who have coding skills. But we will try to explain each step in detail.
To achieve this, we need to modify WooCommerce’s core files.
Editing these files is a delicate process. If you make any mistake, an error will be displayed on your site. Therefore, we recommend using a child theme.
Let us get right into it.
Steps to Add Fees to the WooCommerce Checkout
Here are the steps you need to follow:
- Log into your WordPress site and access the Dashboard as the admin user.
- From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to add an extra charge on the WooCommerce checkout page.
- Add the following code to the PHP file:
add_action('woocommerce_cart_calculate_fees', function() { if (is_admin() && !defined('DOING_AJAX')) { return; } WC()->cart->add_fee(__('Additional Charge', 'txtdomain'), 10); });
- The code snippet above adds a flat fee of $10 called an Additional Charge.
- It will be added to the total of the customers’ checkout page automatically. This is the outcome:
- Alternatively, you can add a percentage fee. If you want to ass this functionality, add the following code:
add_action('woocommerce_cart_calculate_fees', function() { if (is_admin() && !defined('DOING_AJAX')) { return; } $percentage = 0.04; $percentage_fee = (WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total()) * $percentage; WC()->cart->add_fee(__('Tax', 'txtdomain'), $percentage_fee);
Conclusion
In today’s post, we have shared two code snippets to add an additional charge on the checkout page. In addition, we have also highlighted some of the reasons for adding extra fees to the checkout.
It is important to label the extra fee to avoid cart abandonment clearly. We recommend that you be transparent with your fees. Alternatively, you can use a plugin to customize the checkout page.
We hope that this post helped you learn more about how the checkout page works.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Change Shipping Label WooCommerce
- How to Add WooCommerce Products to Facebook Shop
- How to Translate WooCommerce Checkout Page
- How to Customize Product Category Page In WooCommerce
- What is the Difference Between WordPress and WooCommerce Explained
- How to Get Post ID by Slug in WordPress With a Practical Example
- How to Add CAPTCHA WooCommerce Login
- WooCommerce Accept Terms and Conditions Checkbox On Registration Form
- How to Get Current Product WooCommerce
- How to Add View Cart Button In WooCommerce
- How to Set Up WooCommerce Buy One Get One
- How to Add Information to the WooCommerce Shop Page
- How to Add WooCommerce Shipping Calculator on Cart Page
- How to Set Up WooCommerce Upload File on Checkout
- How to Select All Except Last Child In CSS » CSS Not Last Child Example
- What is the Best Payment Gateway for WooCommerce
- How to Get WooCommerce Current Order ID from URL