Skip to content

Commit

Permalink
Merge pull request #24943 from woocommerce/refactor/eu-vat-brexit
Browse files Browse the repository at this point in the history
Brexit and VAT refactor
  • Loading branch information
peterfabian committed Feb 5, 2020
2 parents 92708dd + b385404 commit d3a33f8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
59 changes: 37 additions & 22 deletions includes/class-wc-countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,33 +350,47 @@ public function get_shipping_country_states() {
/**
* Gets an array of countries in the EU.
*
* MC (monaco) and IM (isle of man, part of UK) also use VAT.
*
* @param string $type Type of countries to retrieve. Blank for EU member countries. eu_vat for EU VAT countries.
* @param string $deprecated Function used to return VAT countries based on this.
* @return string[]
*/
public function get_european_union_countries( $type = '' ) {
$countries = array( 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HU', 'HR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' );
public function get_european_union_countries( $deprecated = '' ) {
$countries = array( 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HU', 'HR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' );

if ( 'eu_vat' === $type ) {
$countries[] = 'MC';
$countries[] = 'IM';
if ( ! empty( $deprecated ) ) {
wc_deprecated_argument( 'type', '4.0.0', 'Use the WC_Countries::get_vat_countries method instead.' );
$countries = $this->get_vat_countries();
}

return apply_filters( 'woocommerce_european_union_countries', $countries, $type );
return apply_filters( 'woocommerce_european_union_countries', $countries, $deprecated );
}

/**
* Gets an array of Non-EU countries that use VAT as the Local name for their taxes based on this list - https://en.wikipedia.org/wiki/Value-added_tax#Non-European_Union_countries
*
* @deprecated 4.0.0
* @since 3.9.0
* @return string[]
*/
public function countries_using_vat() {
wc_deprecated_function( 'countries_using_vat', '4.0', 'WC_Countries::get_vat_countries' );
$countries = array( 'AL', 'AR', 'AZ', 'BS', 'BH', 'BY', 'BB', 'BO', 'EG', 'ET', 'CL', 'CO', 'EC', 'SV', 'FJ', 'GM', 'GH', 'GT', 'IN', 'IR', 'IL', 'KZ', 'MU', 'MK', 'MX', 'MD', 'MN', 'ME', 'NA', 'NP', 'NG', 'PS', 'PY', 'RU', 'RW', 'KN', 'SA', 'RS', 'ZA', 'KR', 'LK', 'TH', 'TR', 'UA', 'UY', 'UZ', 'VE', 'VN', 'AE' );

return apply_filters( 'woocommerce_countries_using_vat', $countries );
}

/**
* Gets an array of countries using VAT.
*
* @since 4.0.0
* @return string[] of country codes.
*/
public function get_vat_countries() {
$eu_countries = $this->get_european_union_countries();
$vat_countries = array( 'AE', 'AL', 'AR', 'AZ', 'BB', 'BH', 'BO', 'BS', 'BY', 'CL', 'CO', 'EC', 'EG', 'ET', 'FJ', 'GB', 'GH', 'GM', 'GT', 'IL', 'IM', 'IN', 'IR', 'KN', 'KR', 'KZ', 'LK', 'MC', 'MD', 'ME', 'MK', 'MN', 'MU', 'MX', 'NA', 'NG', 'NO', 'NP', 'PS', 'PY', 'RS', 'RU', 'RW', 'SA', 'SV', 'TH', 'TR', 'UA', 'UY', 'UZ', 'VE', 'VN', 'ZA' );

return apply_filters( 'woocommerce_vat_countries', array_merge( $eu_countries, $vat_countries ) );
}

/**
* Gets the correct string for shipping - either 'to the' or 'to'.
*
Expand Down Expand Up @@ -411,7 +425,7 @@ public function estimated_for_prefix( $country_code = '' ) {
* @return string
*/
public function tax_or_vat() {
$return = in_array( $this->get_base_country(), array_merge( $this->get_european_union_countries( 'eu_vat' ), array( 'NO' ), $this->countries_using_vat() ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
$return = in_array( $this->get_base_country(), $this->get_vat_countries(), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );

return apply_filters( 'woocommerce_countries_tax_or_vat', $return );
}
Expand All @@ -422,7 +436,7 @@ public function tax_or_vat() {
* @return string
*/
public function inc_tax_or_vat() {
$return = in_array( $this->get_base_country(), array_merge( $this->get_european_union_countries( 'eu_vat' ), array( 'NO' ), $this->countries_using_vat() ), true ) ? __( '(incl. VAT)', 'woocommerce' ) : __( '(incl. tax)', 'woocommerce' );
$return = in_array( $this->get_base_country(), $this->get_vat_countries(), true ) ? __( '(incl. VAT)', 'woocommerce' ) : __( '(incl. tax)', 'woocommerce' );

return apply_filters( 'woocommerce_countries_inc_tax_or_vat', $return );
}
Expand All @@ -433,7 +447,7 @@ public function inc_tax_or_vat() {
* @return string
*/
public function ex_tax_or_vat() {
$return = in_array( $this->get_base_country(), array_merge( $this->get_european_union_countries( 'eu_vat' ), array( 'NO' ), $this->countries_using_vat() ), true ) ? __( '(ex. VAT)', 'woocommerce' ) : __( '(ex. tax)', 'woocommerce' );
$return = in_array( $this->get_base_country(), $this->get_vat_countries(), true ) ? __( '(ex. VAT)', 'woocommerce' ) : __( '(ex. tax)', 'woocommerce' );

return apply_filters( 'woocommerce_countries_ex_tax_or_vat', $return );
}
Expand All @@ -458,15 +472,16 @@ public function country_dropdown_options( $selected_country = '', $selected_stat
echo ' selected="selected"';
}

echo '>' . esc_html( $value ) . ' &mdash; ' . ( $escape ? esc_js( $state_value ) : $state_value ) . '</option>'; // WPCS: XSS ok.
echo '>' . esc_html( $value ) . ' &mdash; ' . ( $escape ? esc_js( $state_value ) : $state_value ) . '</option>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

}
echo '</optgroup>';
} else {
echo '<option';
if ( $selected_country === $key && '*' === $selected_state ) {
echo ' selected="selected"';
}
echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value ) : $value ) . '</option>'; // WPCS: XSS ok.
echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value ) : $value ) . '</option>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
Expand Down Expand Up @@ -848,7 +863,7 @@ public function get_country_locale() {
'postcode' => array(
'label' => __( 'Postal code', 'woocommerce' ),
),
'state' => array(
'state' => array(
'label' => __( 'Province', 'woocommerce' ),
),
),
Expand Down Expand Up @@ -1003,30 +1018,30 @@ public function get_country_locale() {
),
),
'JP' => array(
'last_name' => array(
'last_name' => array(
'class' => array( 'form-row-first' ),
'priority' => 10,
),
'first_name' => array(
'class' => array( 'form-row-last' ),
'priority' => 20,
),
'postcode' => array(
'postcode' => array(
'class' => array( 'form-row-first' ),
'priority' => 65,
),
'state' => array(
'state' => array(
'label' => __( 'Prefecture', 'woocommerce' ),
'class' => array( 'form-row-last' ),
'priority' => 66,
),
'city' => array(
'city' => array(
'priority' => 67,
),
'address_1' => array(
'address_1' => array(
'priority' => 68,
),
'address_2' => array(
'address_2' => array(
'priority' => 69,
),
),
Expand Down Expand Up @@ -1265,7 +1280,7 @@ public function get_country_locale() {
'VN' => array(
'state' => array(
'required' => false,
'hidden' => true,
'hidden' => true,
),
'postcode' => array(
'priority' => 65,
Expand Down
21 changes: 21 additions & 0 deletions tests/unit-tests/countries/countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,25 @@ public function test_get_country_locale() {
$this->assertArrayNotHasKey( 'AU', $locales );
$this->assertArrayHasKey( 'default', $locales );
}

/**
* Test get_european_union_countries.
*
* @return void
*/
public function test_get_european_union_countries() {
// After Brexit there should be 27 countries in the EU.
$countries = new WC_Countries();
$this->assertCount( 27, $countries->get_european_union_countries() );
}

/**
* Test get_vat_countries.
*
* @return void
*/
public function test_get_vat_countries() {
$countries = new WC_Countries();
$this->assertCount( 80, $countries->get_vat_countries() );
}
}

0 comments on commit d3a33f8

Please sign in to comment.