Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Tag: Hooks

WooCommerce: Add a handling fee or surcharge

Posted on August 19, 2025 By Admin No Comments on WooCommerce: Add a handling fee or surcharge

WooCommerce: Add a Handling Fee or Surcharge

Adding a handling fee or surcharge to your WooCommerce store can help cover extra costs such as packaging, payment processing, or special delivery requirements. This tutorial shows you how to woocommerce add handling fee quickly and effectively using custom code snippets, ensuring the fee appears transparently during checkout.

Quick Fix: Add a Handling Fee in WooCommerce

  1. Access your WordPress theme’s functions.php file or use a site-specific plugin.
  2. Copy and paste the provided PHP code snippet to add a fixed or percentage-based handling fee.
  3. Save changes and test the checkout to confirm the fee appears correctly.

Why This Happens

WooCommerce by default does not include a handling fee option. Many store owners need to add extra charges to cover costs beyond product prices and shipping. Since WooCommerce’s built-in fees are flexible but require manual input, adding a custom handling fee via code automates this process and ensures consistent application.

Requirements

  • WooCommerce installed and active on your WordPress site.
  • Access to your theme’s functions.php file or a custom plugin for adding PHP code.
  • Basic familiarity with editing WordPress files or using a code snippet plugin.

Step-by-Step: Adding a Handling Fee in WooCommerce

1. Backup Your Site

Before making any code changes, always backup your site to avoid accidental loss or downtime.

2. Open Your Theme’s functions.php File

Use FTP, cPanel File Manager, or the WordPress theme editor to open functions.php located in your active theme folder.

3. Add the Handling Fee Code Snippet

Paste the following code at the end of the functions.php file. This example adds a fixed handling fee of $5.00 to every order:

add_action( 'woocommerce_cart_calculate_fees', 'add_handling_fee' );
function add_handling_fee() {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $handling_fee = 5.00; // Fixed handling fee amount
    WC()-cart-add_fee( 'Handling Fee', $handling_fee, true, '' );
}

4. Save the File

Save your changes and upload the file back to the server if using FTP.

5. Test the Checkout

Add products to your cart and proceed to checkout. You should see the “Handling Fee” line item added to the order totals.

Optional: Percentage-Based Handling Fee

If you prefer a handling fee based on a percentage of the cart subtotal, use this code instead:

add_action( 'woocommerce_cart_calculate_fees', 'add_percentage_handling_fee' );
function add_percentage_handling_fee() {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 0.05; // 5% handling fee
    $cart_subtotal = WC()-cart-get_subtotal();
    $handling_fee = $cart_subtotal * $percentage;

    WC()-cart-add_fee( 'Handling Fee (5%)', $handling_fee, true, '' );
}

Common Pitfalls

  • Fee not showing: Ensure the code is placed in the active theme’s functions.php or a site-specific plugin, and WooCommerce is active.
  • Fee applied multiple times: The code checks for admin and AJAX contexts to prevent duplication. Avoid adding the snippet multiple times.
  • Tax settings: The third parameter in add_fee() controls if the fee is taxable. Set it to true or false depending on your tax rules.
  • Compatibility: Custom code may conflict with other plugins that modify cart fees. Test thoroughly.

Works On

Environment Compatibility
Web Servers Apache, Nginx, LiteSpeed
Hosting Panels cPanel, Plesk, DirectAdmin
WordPress Versions WooCommerce compatible versions (4.0+ recommended)

FAQ

Q1: Can I add different handling fees based on payment method?

Yes. You can check the chosen payment method using WC()-session-get('chosen_payment_method') inside the fee function and conditionally add fees.

Q2: How do I make the handling fee tax-exempt?

Set the third parameter of add_fee() to false. For example: WC()-cart-add_fee( 'Handling Fee', $fee, false );

Q3: Can I add handling fees only for specific products or categories?

Yes. Use WC()-cart-get_cart() to loop through cart items and conditionally apply fees based on product IDs or categories.

Q4: Will this handling fee appear in order emails and invoices?

Yes. WooCommerce includes fees in order totals, so the handling fee will appear in emails, invoices, and admin order details.

Q5: Is there a plugin alternative to adding handling fees?

Yes. Plugins like “WooCommerce Advanced Fees” or “WooCommerce Extra Fees Plugin” offer GUI-based fee management without coding.…

WooCommerce How‑tos

WooCommerce: Redirect after add to cart (to cart or checkout)

Posted on August 19, 2025 By Admin No Comments on WooCommerce: Redirect after add to cart (to cart or checkout)

WooCommerce: Redirect after Add to Cart (to Cart or Checkout)

By default, WooCommerce keeps customers on the product page after they add an item to their cart. Sometimes, you want to improve the shopping experience by redirecting users immediately to the cart or checkout page after adding a product. This tutorial explains how to implement a redirect after add to cart in WooCommerce quickly and reliably.

Quick Fix: Redirect After Add to Cart

  1. Choose whether to redirect to the cart page or the checkout page.
  2. Add a snippet of PHP code to your theme’s functions.php file or a site-specific plugin.
  3. Test the add-to-cart button to confirm the redirect works as expected.

Why This Happens

WooCommerce’s default behavior is designed to keep customers browsing products after adding an item to their cart. This can be good for discovery but sometimes disrupts the flow for customers ready to purchase. Redirecting after add to cart streamlines the checkout process by taking users directly to the cart or checkout page, reducing friction and potentially increasing conversions.

Requirements

  • WooCommerce plugin installed and active.
  • Access to your WordPress theme’s functions.php file or a site-specific plugin to add custom code.
  • Basic knowledge of editing PHP files in WordPress.

Step-by-step: Redirect After Add to Cart

  1. Backup your site. Always back up your site before editing theme files.
  2. Access your theme’s functions.php file. You can do this via Appearance > Theme Editor in WordPress admin or via FTP.
  3. Add the redirect code snippet. Copy and paste one of the following snippets depending on where you want to redirect users.

Code Snippet: Redirect to Cart Page

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_redirect_to_cart' );
function custom_redirect_to_cart() {
    return wc_get_cart_url();
}

Code Snippet: Redirect to Checkout Page

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_redirect_to_checkout' );
function custom_redirect_to_checkout() {
    return wc_get_checkout_url();
}
  1. Save the file. After adding the code, save your changes.
  2. Test the functionality. Visit a product page, add a product to the cart, and verify you are redirected to the cart or checkout page as intended.

Common Pitfalls

  • Theme or plugin conflicts: Some themes or plugins may override WooCommerce’s add to cart behavior, preventing the redirect from working.
  • AJAX add to cart enabled: If your theme uses AJAX for add to cart, the redirect filter may not trigger because the page does not reload.
  • Editing the wrong file: Ensure you add the code to your active theme’s functions.php or a site-specific plugin, not a parent theme if using a child theme.
  • Cache issues: Browser or server caching can interfere with testing changes. Clear caches after updating code.

Works on

Server/Environment Compatibility
Apache Fully compatible
Nginx Fully compatible
LiteSpeed Fully compatible
cPanel Fully compatible
Plesk Fully compatible

FAQ

Q: Why doesn’t the redirect work on my site?
A: If your theme uses AJAX add to cart, the redirect filter won’t trigger because the page doesn’t reload. You may need to disable AJAX add to cart or use JavaScript-based redirects.
Q: Can I redirect to a custom page after add to cart?
A: Yes. Replace wc_get_cart_url() or wc_get_checkout_url() with the URL of your custom page in the redirect function.
Q: Is it safe to add this code to my theme’s functions.php file?
A: Yes, but changes will be lost if you update the theme. Use a child theme or a site-specific plugin to keep changes persistent.
Q: How do I disable the redirect and go back to default behavior?
A: Remove or comment out the redirect filter code from your functions.php file or plugin.
Q: Will this affect variable or grouped products?
A: The redirect applies globally to all products added to the cart. However, AJAX add to cart on variable or grouped products may behave differently depending on your theme.
…
WooCommerce How‑tos

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com