How to Add Product Fees to the Subtotal in WooCommerce
As an e-commerce store owner, you may need to add various fees to your products, such as shipping, handling, or administrative charges. These fees can significantly impact the final price your customers pay and it's crucial to ensure they are accurately reflected in the cart and checkout process.
In this article, we'll explore the step-by-step process of adding product fees to the subtotal in WooCommerce, a leading WordPress e-commerce plugin. We'll cover how to create custom fees, apply them to specific products or the entire cart, and ensure a seamless checkout experience for your customers.
Understanding WooCommerce Fees
WooCommerce provides a built-in feature for adding fees to your store, allowing you to create custom charges that can be applied to individual products or the entire cart. These fees can be used to cover a variety of costs, such as:
- Shipping Fees: Additional charges for shipping products to customers.
- Handling Fees: Fees to cover the administrative costs of processing orders.
- Packaging Fees: Charges for specialized packaging or gift wrapping.
- Taxes: Applicable sales taxes or value-added taxes (VAT).
- Promotional Discounts: Negatively charged fees to offer discounts or promotions.
By adding these fees to the subtotal, you can ensure that your customers are aware of the total cost of their purchases before reaching the checkout page, leading to a more transparent and positive shopping experience.
Step 1: Install and Activate the WooCommerce Plugin
If you haven't already, the first step is to install and activate the WooCommerce plugin on your WordPress website. You can do this by navigating to the "Plugins" section in your WordPress dashboard, searching for "WooCommerce," and clicking the "Install" and "Activate" buttons.
Once WooCommerce is installed and activated, you'll be able to access the plugin's settings and begin configuring your store.
Step 2: Create a Custom Fee
To add a product fee to the subtotal in WooCommerce, you'll need to create a custom fee. Here's how:
- Go to your WordPress dashboard and navigate to "WooCommerce" > "Settings" > "Fees."
- Click on the "Add Fee" button to create a new fee.
- In the "Fee Name" field, enter a descriptive name for your fee, such as "Shipping Fee" or "Handling Charge."
- In the "Fee Amount" field, enter the amount you want to charge for the fee. You can use a fixed amount or a percentage of the subtotal.
- Select the "Tax Status" for your fee. This determines whether the fee should be taxed or not.
- Choose the "Fee Type" – either a "Percentage" or a "Fixed" amount.
- Optionally, you can set the "Fee Applies To" field to determine which products or the entire cart the fee should be applied to.
- Click the "Save changes" button to create your custom fee.
Step 3: Apply the Custom Fee to the Subtotal
Now that you've created a custom fee, you need to ensure that it's applied to the subtotal in the cart and checkout process. WooCommerce provides several hooks and filters that you can use to customize the fee behavior.
Here's an example of how you can add the custom fee to the subtotal:
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee_to_subtotal' );
function add_custom_fee_to_subtotal( $cart ) {
// Get the custom fee you created earlier
$fee = WC()->cart->get_fees()['your_custom_fee_name'];
if ( $fee ) {
// Add the fee to the subtotal
$cart->add_fee( $fee->name, $fee->amount );
}
}
In this example, we're using the woocommerce_cart_calculate_fees
action hook to add the custom fee to the subtotal. The WC()->cart->get_fees()
method retrieves the custom fee you created earlier, and we then use the $cart->add_fee()
method to apply the fee to the subtotal.
Make sure to replace 'your_custom_fee_name'
with the actual name of the fee you created in the previous step.
Step 4: Customize the Fee Display
By default, WooCommerce will display the custom fee as a line item in the cart and checkout pages. However, you may want to customize the way the fee is displayed to your customers. For example, you might want to show the fee as part of the subtotal instead of a separate line item.
Here's an example of how you can modify the fee display:
add_filter( 'woocommerce_get_cart_item_from_session', 'include_custom_fee_in_subtotal', 10, 3 );
function include_custom_fee_in_subtotal( $cart_item, $session_values, $cart_item_key ) {
// Get the custom fee you created earlier
$fee = WC()->cart->get_fees()['your_custom_fee_name'];
if ( $fee ) {
// Include the fee in the subtotal
$cart_item['data']->set_price( $cart_item['data']->get_price() + $fee->amount );
}
return $cart_item;
}
In this example, we're using the woocommerce_get_cart_item_from_session
filter to include the custom fee in the subtotal. We retrieve the custom fee and then add its amount to the product's price, effectively including the fee in the subtotal.
Again, make sure to replace 'your_custom_fee_name'
with the actual name of the fee you created earlier.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.
Step 5: Ensure Accurate Totals and Taxes
When adding custom fees to the subtotal, it's important to ensure that the total order amount and any applicable taxes are calculated correctly. WooCommerce provides several hooks and filters that you can use to customize the tax calculations.
Here's an example of how you can ensure accurate totals and taxes:
add_filter( 'woocommerce_cart_total', 'include_custom_fee_in_cart_total', 10, 1 );
function include_custom_fee_in_cart_total( $total ) {
// Get the custom fee you created earlier
$fee = WC()->cart->get_fees()['your_custom_fee_name'];
if ( $fee ) {
// Include the fee in the cart total
$total += $fee->amount;
}
return $total;
}
add_filter( 'woocommerce_cart_total_ex_tax', 'include_custom_fee_in_cart_total_ex_tax', 10, 1 );
function include_custom_fee_in_cart_total_ex_tax( $total ) {
// Get the custom fee you created earlier
$fee = WC()->cart->get_fees()['your_custom_fee_name'];
if ( $fee ) {
// Include the fee in the cart total (excluding tax)
$total += $fee->amount;
}
return $total;
}
In these examples, we're using the woocommerce_cart_total
and woocommerce_cart_total_ex_tax
filters to include the custom fee in the cart total and the cart total excluding tax, respectively. This ensures that the final order amount and any applicable taxes are calculated correctly.
Again, make sure to replace 'your_custom_fee_name'
with the actual name of the fee you created earlier.
Real-World Example: Shipping Fees in WooCommerce
Let's consider a real-world example of adding a shipping fee to the subtotal in WooCommerce. Suppose you run an online store that sells various products, and you want to charge a flat shipping fee of $5 for each order.
Here's how you can implement this:
-
Create a custom fee in the WooCommerce settings with the following details:
- Fee Name: Shipping Fee
- Fee Amount: 5
- Tax Status: Taxable
- Fee Type: Fixed
-
Add the custom fee to the subtotal using the code snippet provided in step 3:
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee_to_subtotal' );
function add_custom_fee_to_subtotal( $cart ) {
$fee = WC()->cart->get_fees()['Shipping Fee'];
if ( $fee ) {
$cart->add_fee( $fee->name, $fee->amount );
}
}
-
Customize the fee display by including it in the subtotal using the code snippet provided in step 4:
add_filter( 'woocommerce_get_cart_item_from_session', 'include_custom_fee_in_subtotal', 10, 3 );
function include_custom_fee_in_subtotal( $cart_item, $session_values, $cart_item_key ) {
$fee = WC()->cart->get_fees()['Shipping Fee'];
if ( $fee ) {
$cart_item['data']->set_price( $cart_item['data']->get_price() + $fee->amount );
}
return $cart_item;
}
-
Ensure accurate totals and taxes by including the custom fee in the cart total and cart total excluding tax, as shown in step 5:
add_filter( 'woocommerce_cart_total', 'include_custom_fee_in_cart_total', 10, 1 );
function include_custom_fee_in_cart_total( $total ) {
$fee = WC()->cart->get_fees()['Shipping Fee'];
if ( $fee ) {
$total += $fee->amount;
}
return $total;
}
add_filter( 'woocommerce_cart_total_ex_tax', 'include_custom_fee_in_cart_total_ex_tax', 10, 1 );
function include_custom_fee_in_cart_total_ex_tax( $total ) {
$fee = WC()->cart->get_fees()['Shipping Fee'];
if ( $fee ) {
$total += $fee->amount;
}
return $total;
}
With these changes, the $5 shipping fee will be added to the subtotal, and the total order amount (including taxes) will be calculated correctly.
Conclusion
In this comprehensive guide, you've learned how to add product fees to the subtotal in WooCommerce. By creating custom fees, applying them to specific products or the entire cart, and ensuring accurate totals and taxes, you can provide a transparent and seamless checkout experience for your customers.
Remember, the techniques and code snippets provided in this article can be adapted to suit your specific business requirements, such as adding handling fees, packaging charges, or promotional discounts. By leveraging the flexibility of WooCommerce, you can create a tailored e-commerce solution that meets the unique needs of your online store.
For more information on how Flowpoint.ai can help you identify and fix technical issues that impact your website's conversion rates, visit Flowpoint.ai