Adding Discounts or Fees to WooCommerce Orders by Country

Programmatically add fees or discounts for orders shipping to specific countries in PHP

Adding Discounts or Fees to WooCommerce Orders by Country
Shawn Wernig
January 30, 2025
by Shawn Wernig

Offset costs, or promote purchases from other Countries by adding discounts or fees to their order in WooCommerce. This is a quick, handy snippet that will allow you to do just that!

For ease of use you may copy/paste these snippets into your functions.php file, or copy and paste this code into your own plugin. It’s also quite easy to adjust this code to your specific country or discount/fee scheme.

When comparing countries, remember that WooCommerce uses the Alpha-2 country codes (two letter country codes, ex: CA for Canada, US for United States)

add_action( 'woocommerce_cart_calculate_fees', function ( $cart ) {

    // If we're doing ajax, we can skip running this.
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    
    // Get the shipping country:
    $shipping_country = WC()->customer->get_shipping_country();

    // $shipping_country is the Alpha-2 country code: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
    if( strtoupper( $shipping_country ) == 'US' )
    {
        // Calculate your discount - this could be a set dollar amount, or a percentage like we do here
        $discount = WC()->cart->subtotal * (12.5 / 100);

        // Apply the discount as a Fee. Fees can be positive or negative values!
        $cart->add_fee(  '12.5% Discount for orders in the United States', -$discount );

    }


}, 20, 1 );
Eggplant Studios - Custom Website Design and Development
Creating custom web solutions and happy clients since 2002
COPYRIGHT ©
2025
Eggplant Studios
- ALL RIGHTS RESERVED
LOGIN