Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brexit and VAT refactor #24943

Merged
merged 14 commits into from
Feb 5, 2020
64 changes: 42 additions & 22 deletions includes/class-wc-countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,25 +350,24 @@ 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', '3.9.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
*
* @since 3.9.0
* @return string[]
*/
public function countries_using_vat() {
Expand All @@ -377,6 +376,26 @@ public function countries_using_vat() {
return apply_filters( 'woocommerce_countries_using_vat', $countries );
}

/**
* Gets an array of countries using VAT.
*
* @since 4.0.0
* @return array of country codes.
*/
public function get_vat_countries() {
claudiosanches marked this conversation as resolved.
Show resolved Hide resolved
$eu_countries = $this->get_european_union_countries();
$vat_countries = $this->countries_using_vat();
$countries = array(
'GB',
'IM',
'MC',
'NO',
);

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


/**
* Gets the correct string for shipping - either 'to the' or 'to'.
*
Expand Down Expand Up @@ -411,7 +430,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 +441,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 +452,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 +477,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 +868,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 +1023,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 +1285,7 @@ public function get_country_locale() {
'VN' => array(
'state' => array(
'required' => false,
'hidden' => true,
'hidden' => true,
),
'postcode' => array(
'priority' => 65,
Expand Down
32 changes: 32 additions & 0 deletions tests/unit-tests/countries/countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,36 @@ 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() {
claudiosanches marked this conversation as resolved.
Show resolved Hide resolved
$countries = new WC_Countries();
// All EU countries use VAT.
$eu_countries = $countries->get_european_union_countries();

// Additional countries using VAT.
$vat_countries = array(
'GB',
'IM',
'MC',
'NO',
);

$this->assertEquals( array_merge( $eu_countries, $vat_countries ), $countries->get_vat_countries() );
}
}