Halloween Sale 15% off Ends in See the plans
PHP

Reverse Charge VAT for EU Business Buyers

Removes VAT for business customers in another EU country who enter a VAT number, and adds a "reverse charge" note to the order, as cross-border B2B sales require.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_eu_countries = array( 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' );

$wpide_reverse_charge = function ( $country, $vat ) use ( $wpide_eu_countries ) {
    $base = WC()->countries->get_base_country();

    return '' !== trim( (string) $vat ) && $country !== $base && in_array( $country, $wpide_eu_countries, true );
};

add_action( 'woocommerce_checkout_update_order_review', function ( $post_data ) use ( $wpide_reverse_charge ) {
    parse_str( (string) $post_data, $posted );
    WC()->customer->set_is_vat_exempt( $wpide_reverse_charge( (string) ( $posted['billing_country'] ?? '' ), $posted['billing_vat_number'] ?? '' ) );
} );

add_action( 'woocommerce_checkout_process', function () use ( $wpide_reverse_charge ) {
    // phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the checkout nonce.
    $exempt = $wpide_reverse_charge( wc_clean( wp_unslash( $_POST['billing_country'] ?? '' ) ), wc_clean( wp_unslash( $_POST['billing_vat_number'] ?? '' ) ) );
    WC()->customer->set_is_vat_exempt( $exempt );
} );

add_action( 'woocommerce_checkout_create_order', function ( $order ) {
    if ( WC()->customer && WC()->customer->get_is_vat_exempt() && $order->get_meta( '_billing_vat_number' ) ) {
        $order->update_meta_data( '_wpide_reverse_charge', 'yes' );
    }
}, 20 );

add_action( 'woocommerce_email_after_order_table', function ( $order ) {
    if ( 'yes' === $order->get_meta( '_wpide_reverse_charge' ) ) {
        echo '<p>' . esc_html( 'VAT reverse charge: the customer accounts for VAT (Article 196, Council Directive 2006/112/EC).' ) . '</p>';
    }
} );

add_action( 'woocommerce_admin_order_data_after_billing_address', function ( $order ) {
    if ( 'yes' === $order->get_meta( '_wpide_reverse_charge' ) ) {
        echo '<p><strong>' . esc_html( 'VAT reverse charge applied' ) . '</strong></p>';
    }
} );

Notes

Needs the "EU VAT Number Field With Format Check" snippet for the billing_vat_number field; confirm the rules for your business with your accountant.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.