Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add WooCommerce Checkout Fees Only for Specific Product & Payment Methods?

With a lot of products in your online store, you may need to charge extra fees for certain specialty items. Additionally, you might set up additional costs for specific payment gateways when those products are being ordered. If you are seeking a solution to incorporate extra charges for the chosen specific product and payment methods of your customers, this post helps you to achieve both of the above condition.

If you want to add flat or percentage based fees based on payment methods alone, you can check our post on adding fees for different payment methods in WooCoomerce.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Solution: Adding WooCommerce Checkout Fees for Specific Product & Payment Method

In the below code snippet, we have applied additional fees when a particular product (with Product ID 90) is added to cart and payment method “Direct bank transfer” is chosen on the checkout page.

add_action('woocommerce_cart_calculate_fees','ts_custom_add_checkout_fees');
function ts_custom_add_checkout_fees() {
    if (is_checkout()) {
        $chosen_payment_method = WC()->session->get('chosen_payment_method');
        $additional_fee = 0;

        // Define the specific product(s) for which you want to apply the fee
        $target_product_ids = array(90); // Replace with the actual product IDs

        // Define the specific payment method(s) for which you want to apply the fee
        $target_payment_methods = array('bacs'); // Replace with the desired payment method(s)

        // Check if the target product(s) and payment method(s) are present
        $product_found = false;
        $payment_method_found = false;

        foreach (WC()->cart->get_cart() as $cart_item) {
            if (in_array($cart_item['product_id'], $target_product_ids)) {
                $product_found = true;
                break;
            }
        }

        if (in_array($chosen_payment_method, $target_payment_methods)) {
            $payment_method_found = true;
        }

        // Calculate additional fee based on specific product and payment method
        if ($product_found && $payment_method_found) {
            $additional_fee += 2.99; // Replace with the desired fee amount
        }

        // Add the additional fee to the cart
        if ($additional_fee > 0) {
            WC()->cart->add_fee('Additional Fee', $additional_fee);
        }
    }
}

add_action('woocommerce_after_checkout_form', 'ts_custom_refresh_checkout_on_payment_methods_change');
function ts_custom_refresh_checkout_on_payment_methods_change() {
    wc_enqueue_js("
        $('form.checkout').on('change', 'input[name^=\'payment_method\']', function() {
            $('body').trigger('update_checkout');
        });
    ");
}

The provided code adds a custom function, ts_custom_add_checkout_fees, to the woocommerce_cart_calculate_fees action hook. This function is called during the calculation of fees in the WooCommerce cart.

Output:

When the customer selects the specific product (ID:90) and payment method option as “Direct Bank Transfer”, it will calculate and display the additional Fee for Pant as $2.99, where Pant is the Product name.

How to Add WooCommerce Checkout Fees Only for Specific Product & Payment Methods? - Tyche Softwares
How to Add WooCommerce Checkout Fees Only for Specific Product & Payment Methods? - Tyche Softwares

Easily Set Fees & Discounts For Payment Gateways

Finding it difficult to manage all payment gateway fees and adjusting your product pricing accordingly?

The Payment Gateway Based Fees & Discounts plugin is designed to set up fees & discounts for each of your payment modes and simplify your payment process.

Related Article: How to add WooCommerce checkout fees based on shipping method & payment method?

Here’s a breakdown of the code inside the ts_custom_add_checkout_fees function:

  1. It checks if the current page is the checkout page by using the is_checkout() function.
  2. It retrieves the chosen payment method from the WooCommerce session using WC()->session->get(‘choosen_payment_method’).
  3. It initializes the $additional_fee variable to zero.
  4. The code defines specific product IDs in the $target_product_ids array and specific payment methods in the $target_payment_methods array. These are the products and payment methods for which you want to apply the additional fee.
  5. It sets two Boolean variables, $product_found and $payment_method_found, to false initially.
  6. The code then loops through the cart items using WC()->cart->get_cart(). It checks if any of the product IDs in the cart match the specified target product IDs. If a match is found, it sets $product_found to true and breaks out of the loop.
  7. It checks if the chosen payment method matches any of the specified target payment methods. If a match is found, it sets $payment_method_found to true.
  8. If both the product and payment method are found (i.e., $product_found and $payment_method_found are true), it calculates the additional fee. In this example, an additional fee of $2.99 is added to the $additional_fee variable. You can modify this value to the desired fee amount.
  9. If the $additional_fee is greater than zero, it adds the fee to the cart using WC()->cart->add_fee(). The label “Additional Fee” is displayed for the fee in the cart.

The second part of the code snippet is the ts_custom_refresh_checkout_on_payment_methods_change function, hooked to the woocommerce_after_checkout_form action. This function ensures that when there is a change in the payment methods on the checkout form, the checkout is refreshed to recalculate and display the updated fees.

The function uses JavaScript to trigger the update_checkout event on the body element, which initiates the checkout update process.

Conclusion

You can even extend the above code snippet to match multiple payment methods instead of just one by simply adding the payment method ids where you currently have array(‘bacs’).

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x