Skip to content

Commit

Permalink
Updates to 2.9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
WooCommerce committed Sep 13, 2024
1 parent d92d73d commit add9569
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 54 deletions.
9 changes: 9 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
*** EU VAT Number Changelog ***

2024-09-09 - version 2.9.7
* Add - Disable reports & display a notice when HPOS is enabled & syncing is disabled.
* Fix - Ensure the proper IP country is shown in the EU VAT section of the order screen.
* Fix - Ensure the VAT Number Field Label can be properly translated.
* Dev - Bump WooCommerce "tested up to" version 9.2.
* Dev - Bump WooCommerce minimum supported version to 9.0.
* Dev - Log API errors to improve debugging.
* Dev - Fix QIT E2E tests and add support for a few new test types.

2024-07-29 - version 2.9.6
* Dev - Bump WooCommerce "tested up to" version 9.1.
* Dev - Bump WooCommerce minimum supported version to 8.9.
Expand Down
23 changes: 21 additions & 2 deletions includes/class-wc-eu-vat-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,16 @@ public static function output() {
<td>
<?php
if ( $data->ip_country ) {
echo esc_html( $countries[ $data->billing_country ] ) . ' ';
echo esc_html( $countries[ $data->ip_country ] ) . ' ';

if ( $data->billing_country === $data->ip_country ) {
echo '<span style="color:green">&#10004;</span>';
} elseif ( $data->self_declared ) {
esc_html_e( '(self-declared)', 'woocommerce-eu-vat-number' );
printf(
'(%s %s)',
esc_html__( 'User self-declared country as', 'woocommerce-eu-vat-number' ),
esc_html( $countries[ $data->billing_country ] ),
);
} else {
echo '<span style="color:red">&#10008;</span>';
}
Expand Down Expand Up @@ -703,6 +707,21 @@ public static function is_eu_vat_block_adjusted() {
set_transient( $transient_key, $is_adjusted ? 'yes' : 'no', HOUR_IN_SECONDS );
return $is_adjusted;
}

/**
* Get VAT Number field label.
*
* @return string
*/
public static function get_vat_number_field_label() {
$label = get_option( 'woocommerce_eu_vat_number_field_label' );

if ( empty( $label ) ) {
return _x( 'VAT number', 'Default Field Label', 'woocommerce-eu-vat-number' );
}

return $label;
}
}

WC_EU_VAT_Admin::init();
4 changes: 2 additions & 2 deletions includes/class-wc-eu-vat-extend-store-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function vat_number_information() {
$is_valid = $validation_data['validation']['valid'] ?? false;
$error_code = $validation_data['validation']['code'] ?? false;

if ( ( 'reject' === $fail_handler && ! $is_valid ) || 'api' === $error_code ) {
if ( ( 'reject' === $fail_handler && ! $is_valid ) || 'wc-eu-vat-api-error' === $error_code ) {
WC()->session->set( 'vat-number', null );
}

Expand Down Expand Up @@ -378,7 +378,7 @@ private function set_vat_exemption( $validation ) {
case 'accept':
$error_code = $validation['validation']['code'] ?? false;

if ( 'api' !== $error_code ) {
if ( 'wc-eu-vat-api-error' !== $error_code ) {
WC_EU_VAT_Number::maybe_set_vat_exempt( true, $b_country, $s_country );
}
break;
Expand Down
37 changes: 31 additions & 6 deletions includes/class-wc-eu-vat-number.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

require_once __DIR__ . '/vies/class-vies-client.php';
require_once __DIR__ . '/class-wc-eu-vat-uk-number-api.php';
require_once __DIR__ . '/class-wc-eu-vat-admin.php';

/**
* WC_EU_VAT_Number class.
Expand Down Expand Up @@ -112,6 +113,30 @@ public static function init() {
// Add VAT Number in order endpoint (REST API).
add_filter( 'woocommerce_api_order_response', array( __CLASS__, 'add_vat_number_to_order_response' ) );
add_filter( 'woocommerce_rest_prepare_shop_order', array( __CLASS__, 'add_vat_number_to_order_response' ) );

// Logs API errors generated by this plugin.
add_action( 'wp_error_added', array( __CLASS__, 'log_api_errors' ), 10, 2 );
}

/**
* Logs API errors.
*
* @param string $code Error code.
* @param string $message Error message.
*/
public static function log_api_errors( $code, $message ) {
if ( 'wc-eu-vat-api-error' !== $code ) {
return;
}

$logger = wc_get_logger();

$logger->info(
$message,
array(
'source' => 'woocommerce-eu-vat-number',
)
);
}

/**
Expand Down Expand Up @@ -142,7 +167,7 @@ public static function localize_wc_eu_vat_params( $script_handle ) {
array(
'eu_countries' => self::get_eu_countries(),
'b2b_required' => get_option( 'woocommerce_eu_vat_number_b2b', 'false' ),
'input_label' => get_option( 'woocommerce_eu_vat_number_field_label', 'VAT number' ),
'input_label' => WC_EU_VAT_Admin::get_vat_number_field_label(),
'input_description' => get_option( 'woocommerce_eu_vat_number_field_description', '' ),
'failure_handler' => get_option( 'woocommerce_eu_vat_number_failure_handling', 'reject' ),
'use_shipping_country' => wc_eu_vat_use_shipping_country(),
Expand Down Expand Up @@ -231,7 +256,7 @@ public static function vat_number_field( $fields ) {
}

$fields['billing_vat_number'] = array(
'label' => get_option( 'woocommerce_eu_vat_number_field_label', __( 'VAT number', 'woocommerce-eu-vat-number' ) ),
'label' => WC_EU_VAT_Admin::get_vat_number_field_label(),
'default' => $user_id > 0 ? get_user_meta( $user_id, 'vat_number', true ) : '',
'required' => false,
'class' => array(
Expand Down Expand Up @@ -309,7 +334,7 @@ public static function get_ip_country() {
public static function vat_number_is_valid( $vat_number, $country, $postcode = '' ) {
// The StoreAPI will set $vat_number to null if the user does not enter it. We should show an error in this case.
if ( null === $vat_number ) {
return new WP_Error( 'api', __( 'VAT number is required.', 'woocommerce-eu-vat-number' ) );
return new WP_Error( 'wc-eu-vat-api-error', __( 'VAT number is required.', 'woocommerce-eu-vat-number' ) );
}

// Replace unwanted chars on VAT Number.
Expand All @@ -329,7 +354,7 @@ public static function vat_number_is_valid( $vat_number, $country, $postcode = '
// Return error if VAT Country Code doesn't match or exist.
if ( ! isset( self::$country_codes_patterns[ $vat_prefix ] ) || ( $vat_prefix . $vat_number_formatted !== $vat_number ) ) {
// translators: %1$s - VAT number field label, %2$s - VAT Number from user, %3$s - Billing country.
return new WP_Error( 'api', sprintf( __( 'You have entered an invalid country code for %1$s (%2$s) for your country (%3$s).', 'woocommerce-eu-vat-number' ), get_option( 'woocommerce_eu_vat_number_field_label', 'VAT number' ), $vat_number, $country ) );
return new WP_Error( 'wc-eu-vat-api-error', sprintf( __( 'You have entered an invalid country code for %1$s (%2$s) for your country (%3$s).', 'woocommerce-eu-vat-number' ), get_option( 'woocommerce_eu_vat_number_field_label', 'VAT number' ), $vat_number, $country ) );
}

if ( ! empty( $cached_result ) ) {
Expand All @@ -343,7 +368,7 @@ public static function vat_number_is_valid( $vat_number, $country, $postcode = '
$uk_vat_api = new WC_EU_VAT_UK_Number_API();
$is_valid = $uk_vat_api->check_vat_number( $vat_number_formatted );
} catch ( Exception $e ) {
return new WP_Error( 'api', __( 'Error communicating with the VAT validation server - please try again.', 'woocommerce-eu-vat-number' ) );
return new WP_Error( 'wc-eu-vat-api-error', __( 'Error communicating with the VAT validation server - please try again.', 'woocommerce-eu-vat-number' ) );
}
} else {
// Check rest of EU countries with VIES.
Expand All @@ -361,7 +386,7 @@ public static function vat_number_is_valid( $vat_number, $country, $postcode = '
$is_valid = $vies_req->is_valid();

} catch ( SoapFault $e ) {
return new WP_Error( 'api', __( 'Error communicating with the VAT validation server - please try again.', 'woocommerce-eu-vat-number' ) );
return new WP_Error( 'wc-eu-vat-api-error', __( 'Error communicating with the VAT validation server - please try again.', 'woocommerce-eu-vat-number' ) );
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions includes/class-wc-eu-vat-reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,81 @@
*/
class WC_EU_VAT_Reports {

/**
* OrderUtil object.
*
* @var \Automattic\WooCommerce\Utilities\OrderUtil object.
*/
public static $order_util;

/**
* Constructor
*/
public static function init() {
if ( ! function_exists( 'wc_get_container' ) ) {
return;
}

try {
self::$order_util = wc_get_container()->get( Automattic\WooCommerce\Utilities\OrderUtil::class );
} catch ( Exception $e ) {
self::$order_util = false;
}

// The EU VAT reports are incompatible with stores running HPOS with syncing disabled.
if ( self::is_cot_enabled() && ! self::is_cot_sync_enabled() ) {
add_action( 'admin_notices', array( __CLASS__, 'display_hpos_incompatibility_notice' ) );
return;
}

add_action( 'woocommerce_admin_reports', array( __CLASS__, 'init_reports' ) );
}

/**
* Helper function to get whether custom order tables are enabled or not.
*
* @return bool
*/
public static function is_cot_enabled() {
return self::$order_util && self::$order_util::custom_orders_table_usage_is_enabled();
}

/**
* Helper function to check whether custom order tables are in sync or not.
*
* @return bool
*/
public static function is_cot_sync_enabled() {
return self::$order_util && self::$order_util::is_custom_order_tables_in_sync();
}

/**
* Displays an admin notice indicating EU VAT reports are disabled on HPOS environments with no syncing.
*/
public static function display_hpos_incompatibility_notice() {
$screen = get_current_screen();

// Only display the admin notice on report admin screens.
if ( ! $screen || 'woocommerce_page_wc-reports' !== $screen->id ) {
return;
}

if ( current_user_can( 'activate_plugins' ) ) {
/* translators: %1$s: Minimum version %2$s: Plugin page link start %3$s Link end */
printf(
'<div class="notice notice-error"><p><strong>%s</strong></p><p>%s</p></div>',
esc_html__( 'WooCommerce EU VAT Number - Reports Not Available', 'woocommerce-eu-vat-number' ),
sprintf(
// translators: placeholders $1 and $2 are opening <a> tags linking to the WooCommerce documentation on HPOS and data synchronization. Placeholder $3 is a closing link (<a>) tag.
esc_html__( 'EU VAT reports are incompatible with the %1$sWooCommerce data storage features%3$s enabled on your store. Please enable %2$stable synchronization%3$s if you wish to use EU VAT reports.', 'woocommerce-eu-vat-number' ),
'<a href="https://woocommerce.com/document/high-performance-order-storage/">',
'<a href="https://woocommerce.com/document/high-performance-order-storage/#synchronization">',
'</a>',
)
);
}
}

/**
* Add reports
*
Expand Down
2 changes: 1 addition & 1 deletion includes/vies/class-vies-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function get_soap_client() {
$soap_parameters
);
} catch ( Exception $e ) {
return new WP_Error( 'api', __( 'Error communicating with the VAT validation server - please try again.', 'woocommerce-eu-vat-number' ) );
return new WP_Error( 'wc-eu-vat-api-error', __( 'Error communicating with the VAT validation server - please try again.', 'woocommerce-eu-vat-number' ) );
}
}

Expand Down
Loading

0 comments on commit add9569

Please sign in to comment.