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 Method & Postal Code in WooCommerce?

Shipping is one of the important factors to be considered while selling online products. Setting up shipping zones in your WooCommerce store is easy with the built-in feature in WooCommerce settings. Further, you can narrow down your options and even set up a specific zip code/postal code also.

Let’s consider a store owner operating a grocery delivery service. He might want to restrict delivery to specific cities or postal codes considering the high costs associated with delivery. On the other hand, an online electronic store with a wide marketplace around the world would prefer to cover a wide range of postal codes with customizations. In both cases, the stores might need to add certain fees based on the postal code or might want to provide percent based discount on payment method. In this post let’s see how to add Checkout fees based on payment method & Postal Code 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

You need to decide two things before you start implementing the below code. 

  • You will need to collect either individual postal codes or a range of postal codes that correspond to the regions you wish to target.
  • Also, choose the payment method to which you will charge the custom fees.

Solution: WooCommerce Checkout fees based on payment method & postal code

USE CASE 1: Let’s consider an online store that sells electronic products. In certain instances, store owners may restrict the application of fees to specific postal codes such as 560043, 560050, 560051, 560052, and 560055, particularly when customers opt for the Cash on Delivery (COD) payment method.

add_action( 'woocommerce_cart_calculate_fees', 'ts_add_checkout_fee_for_gateway' );

function ts_add_checkout_fee_for_gateway() {
    // Get the chosen payment method
    $chosen_gateway = WC()->session->get( 'chosen_payment_method' );

    // List of specific pincodes where the fee should be added
    $specific_pincodes = array( '560043', '560050', '560051', '560052', '560055' );

    // Check if the chosen payment method is 'cod' and the shipping postcode is in the specific list
    if ( $chosen_gateway == 'cod' && in_array( WC()->customer->get_shipping_postcode(), $specific_pincodes ) ) {

        // Add a fee named 'Delivery Fee' with an amount of 5 to the cart
        WC()->cart->add_fee( 'Delivery Fee', 5 );
    }
}

USE CASE 2: Whereas, some store owners prefer to add fees to a range of postal codes ranging from 560043 to 560098, and only when the Cash on Delivery (COD) payment method is selected. If the customer’s postal code is not within the specified postcodes, the fee is not added.

add_action( 'woocommerce_cart_calculate_fees', 'ts_add_checkout_fee_for_gateway' );

function ts_add_checkout_fee_for_gateway() {
    $chosen_gateway = WC()->session->get( 'chosen_payment_method' );
    if ( $chosen_gateway == 'cod' && in_array( WC()->customer->get_shipping_postcode(), range( 560043, 560098 ) ) ) {
        WC()->cart->add_fee( 'Delivery Fee', 5 );
    }
}

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');
        });
    ");
}

Code Output:

Scenario 1

The below output reveals that the order has to be shipped to the specific postal code 560043 and the customer chooses Cash on Delivery. When both conditions are met, the extra fee of $5 is added to the order subtotal.

How to Add Checkout Fees Based on Payment Method & Postal Code in WooCommerce? - Tyche Softwares

Scenario 2

In another scenario, if the order is received from 560043 the extra fee is not included in the order subtotal. It is because the customer chooses the Direct bank transfer(BACS) payment method, which is not the predefined condition we mentioned for the extra fee.

How to Add Checkout Fees Based on Payment Method & Postal Code in WooCommerce? - Tyche Softwares

Scenario 3

And below output shows that the pincode 560098 falls within the range of pincodes and the user had opt in for Cash on Delivery (COD) payment method, and therefore the fees is added.Selecting any pincode within the range of 560043 to 560098 produces identical results.

How to Add Checkout Fees Based on Payment Method & Postal Code in WooCommerce? - Tyche Softwares

Scenario 4

Let’s assume that the order is received from the postal code 560020 and the customer chooses either cash on delivery or any other payment method. The additional fee will not be added since the postal code 560020 is neither in the array nor in the range of postal codes.

How to Add Checkout Fees Based on Payment Method & Postal Code in WooCommerce? - Tyche Softwares

Code Explanation

Let’s know how the code snippets work.

  1. The action hook woocommerce_cart_calculate_fees triggers the function ts_add_checkout_fee_for_gateway() when WooCommerce calculates cart fees during the checkout process. 
  2. The function ts_add_checkout_fee_for_gateway() is responsible for adding the checkout fee based on the chosen payment gateway and the customer’s shipping postal code.
  3. WC()->session->get(‘chosen_payment_method’) retrieves the chosen payment method from the WooCommerce session. It stores the chosen payment method in the variable $chosen_gateway.
  4. In the first code snippet, creates an array named $specific_pincodes and use the in_array(), which contains specific postal codes (pin codes) where the delivery fee should be added & check if the chosen payment gateway is ‘cod’.
  5. In the second code snippet, we are utilizing the range() function to determine if the customer’s shipping postal code falls within the range of 560043 to 560098 & checks if the chosen payment gateway is ‘cod’.
  6. If the conditions are met, it adds a delivery fee of $5 to the cart using WC()->cart->add_fee(‘Delivery Fee’, 5).
  7. This action hook woocommerce_after_checkout_form triggers the function ts_refresh_checkout_on_payment_methods_change().
  8. The function ts_refresh_checkout_on_payment_methods_change() is responsible for refreshing the checkout page whenever the customer changes the selected payment method.

Read Related Article: How to display a $0.00 amount for free shipping methods in WooCommerce?

Conclusion

The above code helps you to add fees for the defined array or range of postal codes and the Cash on the Delivery (COD) payment method. So, instead of adding fees, you can also modify the code to apply discounts for specific payment methods and postal codes.

We hope you will find this article useful. Let us know how the code snippet works for you in the comment section below.

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

Share It:

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