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

How to add Checkout fees based on Payment Gateway & Specific Country in WooCommerce?

As the majority of people shifted towards online shopping it is no wonder that shopping across nations is easy and fast. As a WooCommerce store owner, you might want to consider International shipping in your online store to reach a global audience. 

International shipping will always incur higher shipping costs and longer delivery times compared to domestic shipping. To address these drawbacks, offering limited-time promotions for orders from specific countries can be effective. Additionally, you may add extra fees based on payment methods to cover additional costs imposed by payment processors and banks for currency conversion charges while reaching global audiences.

This post will act as a complete guide on adding  Checkout fees based on payment gateway & Specific Country in WooCommerce.

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.

Preliminary Steps to adding Checkout fees based on Payment Gateway & Specific Country in WooCommerce:

1. Set up your desired shipping zone and the shipping methods. To set up navigate to WooCommerce->Settings->Shipping->Add Shipping zone.

2. Choose the payment gateway to which you want to add the Global Delivery Surcharge.

Solution: WooCommerce Checkout fees based on payment gateway & Specific Country

Let’s consider the example of an online art gallery store that sells hand-painted artwork. A customer from Germany is looking for his favorite hand-painted artwork and visits the online art gallery store. In such cases, the store owner should have already configured the shipping zone and the shipping methods for that specific country Germany. Otherwise, the customer might receive an error message on the checkout page. You can create the shipping zone as shown below.

How to add Checkout fees based on Payment Gateway & Specific Country in WooCommerce? - Tyche Softwares

The code given below adds a Global Delivery Surcharge of $30  when the order is placed from the specific country Germany ( the country code is “DE”) and the Direct bank transfer (BACS) payment method is chosen.

add_action( 'woocommerce_cart_calculate_fees', 'ts_add_checkout_fee_for_gateway_and_country' );

function ts_add_checkout_fee_for_gateway_and_country() {
    $chosen_gateway = WC()->session->get( 'chosen_payment_method' );
    $shipping_country = WC()->customer->get_shipping_country();
    
    
    
    // Add fee for "BACS" gateway and specific country (e.g., Germany with code "DE")
    if ( $chosen_gateway === 'bacs' && $shipping_country === 'DE' ) {
        WC()->cart->add_fee( 'Global Delivery Surcharge', 30 );
    }
}
add_action( 'woocommerce_after_checkout_form', 'ts_refresh_checkout_on_payment_methods_change' );

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

Output 1

The output depicts that the customer’s shipping country is set to Germany, and the customer has chosen the Direct Bank transfer (BACS) transfer method. As per the code, the condition is met and the Global Delivery Surcharge of $30 is added to the order subtotal.

How to add Checkout fees based on Payment Gateway & Specific Country in WooCommerce? - Tyche Softwares

Output 2

In the below output, the order is placed from India. Since it doesn’t match our predefined geographical location conditions, regardless of any payment method chosen the international delivery fee is not added.

How to add Checkout fees based on Payment Gateway & Specific Country in WooCommerce? - Tyche Softwares

Code Explanation

  • The add_action function is used to hook the function ts_add_checkout_fee_for_gateway_and_country into the woocommerce_cart_calculate_fees action.
  • The function ts_add_checkout_fee_for_gateway_and_country will be executed whenever WooCommerce calculates the cart fees.
  • The ts_add_checkout_fee_for_gateway_and_country function is defined to calculate and add an international shipping fee based on the chosen payment method and the customer’s shipping country.
  • The variable $chosen_gateway retrieves the chosen payment method from the WooCommerce session.
  • The variable $shipping_country retrieves the shipping country of the customer using WC()->customer->get_shipping_country().
  • The function checks if the chosen payment method is ‘bacs’ (Bank Transfer) and if the shipping country is ‘DE’ (Germany).
  • The if statement uses && (logical AND) to ensure that both conditions are true for the fee to be added.
  • If the conditions are met, the function calls WC()->cart->add_fee() to add the fee to the cart.
  • The woocommerce_after_checkout_form action ensures that when customers change their payment method selection during checkout, the function ts_refresh_checkout_on_payment_methods_change will be triggered.
  • As a next step, the ts_refresh_checkout_on_payment_methods_change  function is defined to refresh the checkout form when the payment method is changed.
  • It uses JavaScript (jQuery) to listen for changes in the payment method input and trigger the update_checkout event, which refreshes the checkout form dynamically.

Conclusion

Overall, the code is intended to apply international shipping fees to a specific country (Germany, in this case) and when the customer selects the “Bank Transfer” (BACS) payment gateway during checkout. If you are familiar with coding, you could set up additional countries & payment methodsYou can even adjust the code to set order total or product categories and you can apply the fees accordingly.

Please share your thoughts in the comments section regarding the usefulness of the code in implementing this specific feature.

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