Adding a Custom Fee to WooCommerce Checkout Based on Product Category

Adding a Custom Fee to WooCommerce Checkout Based on Product Category

WooCommerce provides a flexible platform for customizing your online store. One useful customization is adding a custom fee to the checkout process based on the product category in the cart. This can be particularly useful if you need to charge extra for certain types of products, such as those in a “custom fee” category.

In this blog post, we will guide you through the steps to add a custom fee to the WooCommerce checkout if a product from the “custom fee” category is in the cart. We will include the necessary code snippets and explanations to help you implement this feature.

Step 1: Add the Custom Fee Function

First, we need to add a function that checks if there are any products from the “custom fee” category in the cart and then adds the custom fee accordingly. Place this code in your theme’s functions.php file or a custom plugin.

// Add a custom fee if a product from the "custom fee" category is in the cart
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee_based_on_category' );

function add_custom_fee_based_on_category() {
    global $woocommerce;

    // Define the category for which the custom fee should be applied
    $custom_fee_category = 'custom fee';

    // Define the custom fee amount
    $custom_fee_amount = 10; // Change this to the desired fee amount

    // Check if any product from the defined category is in the cart
    $category_found = false;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = wc_get_product( $cart_item['product_id'] );
        if ( has_term( $custom_fee_category, 'product_cat', $product->get_id() ) ) {
            $category_found = true;
            break;
        }
    }

    // If a product from the category is found, add the custom fee
    if ( $category_found ) {
        WC()->cart->add_fee( __( 'Custom Fee', 'woocommerce' ), $custom_fee_amount );
    }
}

Step 2: Customize the Fee Amount and Category

In the code snippet above, you can customize the following parameters:

  • $custom_fee_category: Set this variable to the slug of the product category for which you want to apply the custom fee.
  • $custom_fee_amount: Set this variable to the amount you want to charge as a custom fee.

Step 3: Add the Custom Fee Label Translation

To ensure the custom fee label is translatable, wrap the label in the __() function, as shown in the code above. You can replace 'Custom Fee' with any other label that suits your needs.

Step 4: Testing the Custom Fee

After adding the code to your functions.php file or custom plugin, it’s important to test the functionality:

  1. Add a Product from the “Custom Fee” Category: Add a product that belongs to the “custom fee” category to the cart.
  2. Proceed to Checkout: Go to the checkout page and verify that the custom fee is applied correctly.
  3. Remove the Product: Remove the product from the cart and ensure that the custom fee is no longer applied.

Conclusion

By following these steps, you can easily add a custom fee to the WooCommerce checkout process based on the product category. This customization can help you manage additional charges for specific products and improve your store’s functionality.

Feel free to modify the code to fit your specific requirements. Happy customizing!

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.