pronamic / woocommerce-eu-vat-number

WooCommerce EU VAT Number, Git-ified. Synced manual! This repository is just a mirror of the WooCommerce EU VAT Number plugin. Please do not send pull requests and issues.
4 stars 0 forks source link

Tax calculation and exemption based on shipping address incorrect for orders without shipping address #1

Open remcotolsma opened 4 days ago

remcotolsma commented 4 days ago

For virtual products, customers do not necessarily have to provide a shipping address at checkout. If the tax calculation and exemption is based on the shipping address, problems arise.

https://github.com/pronamic/woocommerce-eu-vat-number/blob/f0ddf1555db737751bed30fb2c0f253b5305053a/includes/class-wc-eu-vat-admin.php#L423-L429

https://github.com/pronamic/woocommerce-eu-vat-number/blob/f0ddf1555db737751bed30fb2c0f253b5305053a/includes/class-wc-eu-vat-number.php#L454-L486

If $shipping_country is an empty string '' the WC_EU_VAT_Number::is_base_country_match( $billing_country, $shipping_country ) method will return false. I think this method should fallback to the $billing_country if the shipping country is empty.

Now when admins press the "Recalculate" button in the WordPress admin dashboard, the tax is removed from such orders. In our case, this goes completely wrong in combination with WooCommerce Subscriptions renewal orders.

https://github.com/woocommerce/woocommerce/blob/9f26128dc2a223d5b9e5482e6a41b5052bfc91c4/plugins/woocommerce/includes/abstracts/abstract-wc-order.php#L1780

remcotolsma commented 4 days ago

The following code might be a quick fix:


\add_filter(
    'woocommerce_order_is_vat_exempt',
    function ( $is_vat_exempt, $order ) {
        if ( ! \method_exists( 'WC_EU_VAT_Number', 'is_base_country_match' ) ) {
            return $is_vat_exempt;
        }

        if ( 'yes' === \get_option( 'woocommerce_eu_vat_number_deduct_in_base', 'yes' ) ) {
            return $is_vat_exempt;
        }

        if ( false === $is_vat_exempt ) {
            return $is_vat_exempt;
        }

        $billing_country  = isset( $_POST['_billing_country'] ) ? wc_clean( wp_unslash( $_POST['_billing_country'] ) ) : $order->get_billing_country();
        $shipping_country = isset( $_POST['_shipping_country'] ) ? wc_clean( wp_unslash( $_POST['_shipping_country'] ) ) : $order->get_shipping_country();

        if ( '' === $shipping_country ) {
            $shipping_country = $billing_country;
        }

        $base_country_match = WC_EU_VAT_Number::is_base_country_match( $billing_country, $shipping_country );

        if ( $base_country_match ) {
            $is_vat_exempt = false;
        }

        return $is_vat_exempt;
    },
    1000,
    2
);
remcotolsma commented 4 days ago

I informed support@woo.com about this in https://secure.helpscout.net/conversation/2736942115/27854#thread-8255663813.