Adding a Custom Fee to WooCommerce Checkout for Specific Products

Adding custom fees to the WooCommerce checkout process can be a powerful way to handle additional charges for specific products. This blog post will guide you through adding a custom fee when a particular product is in the cart using WooCommerce hooks and filters.

Step-by-Step Guide

Prerequisites

  • A running WooCommerce store.
  • Basic knowledge of PHP and WordPress/WooCommerce hooks.

Step 1: Identify the Product ID

First, you need to know the ID of the product for which you want to add a custom fee. You can find the product ID by going to the WooCommerce Products page and hovering over the product name.

Step 2: Add Custom Code to Your Theme

We will add the custom code to the functions.php file of your active theme. You can access this file via the WordPress admin dashboard or using FTP.

Step 3: Write the Code

Below is the code to add a custom fee when a specific product is in the cart:

// Add custom fee for specific product in WooCommerce
function add_custom_fee_for_specific_product( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    // Set the product ID for which the custom fee should be applied
    $target_product_id = 123; // Replace 123 with your product ID
    $fee_amount = 10; // Set the custom fee amount

    // Loop through the cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check if the target product is in the cart
        if ( $cart_item['product_id'] == $target_product_id ) {
            // Add the custom fee
            $cart->add_fee( __( 'Custom Fee', 'woocommerce' ), $fee_amount );
            break; // No need to add the fee more than once
        }
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee_for_specific_product' );

Step 4: Save and Test

After adding the code, save the functions.php file and test your WooCommerce checkout process:

  1. Add the specific product (with the ID you specified) to your cart.
  2. Proceed to the checkout page.
  3. You should see the custom fee applied to the order.

Explanation of the Code

  • Hooking into the Checkout Process: We use the woocommerce_cart_calculate_fees hook to add our custom fee logic during the cart calculation.
  • Admin Check: The code first checks if the current request is an admin request and not an AJAX request to avoid unnecessary execution.
  • Product ID and Fee Amount: We set the target product ID and the custom fee amount.
  • Cart Loop: We loop through the cart items to check if the target product is in the cart.
  • Add Fee: If the target product is found, we add the custom fee using the add_fee method.

Conclusion

By following these steps, you can easily add a custom fee to the WooCommerce checkout process for specific products. This approach can be extended and customized further to meet various business requirements, such as applying different fees based on product categories, quantities, or customer roles.

Feel free to leave a comment below if you have any questions or need further assistance!

Happy coding!

Get A No Obligation Quote

Do You Need Help With Your WooCommerce Site?

Click through to the next page and complete the form to get a free no obligation quote to fix any issue you are having with your WooCommerce site.