diff --git a/README.md b/README.md
index a072b66..dfa436e 100644
--- a/README.md
+++ b/README.md
@@ -44,6 +44,8 @@ If you need to set it up in your theme or plugin, you can use following filters
add_filter( 'woolab_icdic_vies_check', '__return_true' );
+ add_filter( 'woolab_icdic_ignore_check_fail', '__return_true' );
+
add_filter( 'woolab_icdic_vat_exempt_enabled', function(){
return "no"; // or "yes"
} );
diff --git a/assets/js/admin.js b/assets/js/admin.js
index 7a54758..64f41b4 100644
--- a/assets/js/admin.js
+++ b/assets/js/admin.js
@@ -7,9 +7,18 @@
var vies_check = $('#woolab_icdic_vies_check');
if (ares_check.length) {
- enableActive(ares_check);
+ enableAresActive(ares_check);
+ enableIgnoreCheckFailActive(ares_check, vies_check);
ares_check.change(function () {
- enableActive(ares_check);
+ enableAresActive(ares_check);
+ enableIgnoreCheckFailActive(ares_check, vies_check);
+ });
+ }
+
+ if (vies_check.length) {
+ enableIgnoreCheckFailActive(ares_check, vies_check);
+ vies_check.change(function () {
+ enableIgnoreCheckFailActive(ares_check, vies_check);
});
}
@@ -31,7 +40,7 @@
}
});
- function enableActive(ares_check) {
+ function enableAresActive(ares_check) {
var active = $('#woolab_icdic_ares_fill');
if (ares_check.prop("checked") == true) {
@@ -40,4 +49,14 @@
active.prop("disabled", true).prop("checked", false);
}
}
+
+ function enableIgnoreCheckFailActive(ares_check, vies_check) {
+ var checkbox = $('#woolab_icdic_ignore_check_fail');
+
+ if (ares_check.prop('checked') || vies_check.prop('checked')) {
+ checkbox.prop('disabled', false);
+ } else {
+ checkbox.prop('disabled', true).prop('checked', false);
+ }
+ }
})(jQuery);
\ No newline at end of file
diff --git a/assets/js/admin.min.js b/assets/js/admin.min.js
index a1819b5..98dbe0c 100644
--- a/assets/js/admin.min.js
+++ b/assets/js/admin.min.js
@@ -1 +1 @@
-"use strict";!function(c){function i(i){var o=c("#woolab_icdic_ares_fill");1==i.prop("checked")?o.prop("disabled",!1):o.prop("disabled",!0).prop("checked",!1)}c(document).ready(function(){var o=c("#woolab_icdic_ares_check"),e=c("#woolab_icdic_vies_check");o.length&&(i(o),o.change(function(){i(o)})),e.length&&(woolab.soap||e.prop("disabled",!0).prop("checked",!1)),c(".woolab-icdic-notice").length&&c(".woolab-icdic-notice").on("click",".notice-dismiss",function(){c.ajax({url:ajaxurl,data:{action:"woolab_icdic_notice_dismiss"}})})})}(jQuery);
\ No newline at end of file
+"use strict";!function(c){function o(o){var i=c("#woolab_icdic_ares_fill");1==o.prop("checked")?i.prop("disabled",!1):i.prop("disabled",!0).prop("checked",!1)}function i(o,i){var e=c("#woolab_icdic_ignore_check_fail");o.prop("checked")||i.prop("checked")?e.prop("disabled",!1):e.prop("disabled",!0).prop("checked",!1)}c(document).ready(function(){var e=c("#woolab_icdic_ares_check"),a=c("#woolab_icdic_vies_check");e.length&&(o(e),i(e,a),e.change(function(){o(e),i(e,a)})),a.length&&(i(e,a),a.change(function(){i(e,a)})),a.length&&(woolab.soap||a.prop("disabled",!0).prop("checked",!1)),c(".woolab-icdic-notice").length&&c(".woolab-icdic-notice").on("click",".notice-dismiss",function(){c.ajax({url:ajaxurl,data:{action:"woolab_icdic_notice_dismiss"}})})})}(jQuery);
\ No newline at end of file
diff --git a/assets/js/public.js b/assets/js/public.js
index 5a3d24f..b005ecd 100644
--- a/assets/js/public.js
+++ b/assets/js/public.js
@@ -233,6 +233,7 @@
if (woolab.ares_fill) {
// Compatibility with Fluid Checkout for WooCommerce – Lite
+ // https://wordpress.org/support/topic/compatibility-with-kybernaut-ico-dic-plugin/
if ($('#billing_same_as_shipping') && $('#billing_same_as_shipping').is(':checked')) {
// Check whether the CollapsibleBlock library is available
if (window.CollapsibleBlock) {
@@ -290,7 +291,10 @@
}
ares_remove_disabled_from_input();
- ico_class.append('' + data.error + '');
+
+ if (!data.internal_error || !woolab.ignore_check_fail) {
+ ico_class.append('' + data.error + '');
+ }
}
} else {
ares_error(ico_class);
@@ -325,11 +329,14 @@
function ares_error(ico_class) {
if (woolab.ares_fill) {
- $('#billing_company').val('');
- $('#billing_dic').val('');
- $('#billing_postcode').val('');
- $('#billing_city').val('');
- $('#billing_address_1').val('');
+ if (!woolab.ignore_check_fail) {
+ $('#billing_company').val('');
+ $('#billing_dic').val('');
+ $('#billing_postcode').val('');
+ $('#billing_city').val('');
+ $('#billing_address_1').val('');
+ }
+
ares_remove_disabled_from_input();
}
diff --git a/assets/js/public.min.js b/assets/js/public.min.js
index 9f060f3..6c2560f 100644
--- a/assets/js/public.min.js
+++ b/assets/js/public.min.js
@@ -1 +1 @@
-"use strict";!function(i){var l="";function e(){var e;switch(woolab.ares_fill&&(i(".woolab-ic-dic-tip").remove(),c()),i("#billing_ic_field").slideDown(),i("#billing_dic_field").slideDown(),i("#billing_country").val()){case"SK":i("#billing_dic_dph_field").slideDown(),i("#billing_dic_field > label").addClass("woolab-ic-dic-required");break;case"CZ":i("#billing_dic_dph_field").slideUp(),i("#billing_dic_field > label").removeClass("woolab-ic-dic-required");break;default:i("#billing_dic_dph_field").slideUp(),i("#billing_dic_field > label").removeClass("woolab-ic-dic-required")}woolab.ares_check&&(t(e=i("#billing_ic")),i(document.body).on("focusin","#billing_ic",function(){l=e.val()}),e.donetyping(function(){(e=i("#billing_ic")).val()!==l&&t(e)},500)),i("#billing_company_field").slideDown()}function o(){var l="SK"==i("#billing_country").val()?"billing_dic_dph":"billing_dic";i("#billing_company, #billing_ic, #billing_dic, #billing_dic_dph").each(function(e,o){o.getAttribute("data-value")&&(o.value=o.getAttribute("data-value"),o.id==l&&o.value.length&&i(document.body).trigger("update_checkout"))})}function n(i){i.removeClass("kbnt-wrong").removeClass("woocommerce-invalid")}function a(i){i.addClass("kbnt-wrong").addClass("woocommerce-invalid").removeClass("woocommerce-validated")}function c(){i("#billing_company").removeAttr("readonly"),i("#billing_dic").removeAttr("readonly"),i("#billing_postcode").removeAttr("readonly"),i("#billing_city").removeAttr("readonly"),i("#billing_address_1").removeAttr("readonly")}function t(e){if("CZ"===i("#billing_country").val()){var o=e.val();if(o!==l){var t=i("#billing_ic_field"),r=''+woolab.l18n_not_valid+"";i(".woolab-ic-dic-tip").remove(),n(t),t.removeClass("kbnt-ok").removeClass("woocommerce-validated"),7!=o.length&&8!=o.length||null==o.match(/^[0-9]+$/)?(c(),o.length>0?a(t):n(t)):i.ajax({url:woolab.ajaxurl,data:{action:"ajaxAres",ico:o},beforeSend:function(){t.addClass("kbnt-validating"),t.append(''+woolab.l18n_validating+"")},success:function(l){if(t.removeClass("kbnt-validating"),l)if(0==(l=JSON.parse(l)).error){if(i(".woolab-ic-dic-tip").remove(),t.addClass("kbnt-ok").addClass("woocommerce-validated").removeClass("woocommerce-invalid"),woolab.ares_fill){if(i("#billing_same_as_shipping")&&i("#billing_same_as_shipping").is(":checked")&&window.CollapsibleBlock){document.querySelector("#billing_same_as_shipping").checked=!1;var e=document.querySelector("#woocommerce-billing-fields__field-wrapper");e&&CollapsibleBlock.expand(e);var o=document.querySelector("#fc-expansible-form-section__toggle-plus--billing_company"),n=document.querySelector("#fc-expansible-form-section__content--billing_company");o&&CollapsibleBlock.collapse(o),n&&CollapsibleBlock.expand(n);var a=document.querySelector("#fc-expansible-form-section__toggle-plus--billing_dic"),s=document.querySelector("#fc-expansible-form-section__content--billing_dic");a&&CollapsibleBlock.collapse(a),s&&CollapsibleBlock.expand(s)}i("#billing_company").val(l.spolecnost).attr("readonly",!0),i("#billing_dic").val(l.dic).attr("readonly",!0),i("#billing_address_1").val(l.adresa).attr("readonly",!0),i("#billing_postcode").val(l.psc).attr("readonly",!0),i("#billing_city").val(l.mesto).attr("readonly",!0),t.append(''+woolab.l18n_ok+""),i("body").trigger("update_checkout")}}else d(t),i(".woolab-ic-dic-tip").length>0&&i(".woolab-ic-dic-tip").remove(),c(),t.append(''+l.error+"");else d(t),0==i(".woolab-ic-dic-tip").length&&(c(),t.append(r))},error:function(l){0==i(".woolab-ic-dic-tip").length&&(e.val(""),d(t),t.append(''+woolab.l18n_error+""))}}),l=e.val()}}}function d(l){woolab.ares_fill&&(i("#billing_company").val(""),i("#billing_dic").val(""),i("#billing_postcode").val(""),i("#billing_city").val(""),i("#billing_address_1").val(""),c()),a(l)}i.fn.extend({donetyping:function(l,e){e=e||1e3;var o,n=function(i){o&&(o=null,l.call(i))};return this.each(function(l,a){var c=i(a);c.is(":input")&&i(document.body).on("keyup keypress paste",a,function(i){"keyup"==i.type&&8!=i.keyCode||(o&&clearTimeout(o),o=setTimeout(function(){n(a)},e))}),c.is(":input")&&i(document.body).on("blur",a,function(){n(a)})})}}),i(document).ready(function(){i(document.body).on("input",".woolab-ic-dic-no_spaces input",function(){i(this).val(function(i,l){return l.replace(/\s+/g,"")})});var l=i("#billing_iscomp");l.length&&!l.prop("checked")||(o(),e()),i(document.body).on("change","#billing_country, #billing_iscomp",function(){var l,n=i("#billing_iscomp");!n.length||n.prop("checked")?(o(),e()):(i(".woolab-ic-dic-toggle").slideUp(),l="SK"==i("#billing_country").val()?"billing_dic_dph":"billing_dic",i("#billing_company, #billing_ic, #billing_dic, #billing_dic_dph").each(function(e,o){o.setAttribute("data-value",o.value),o.id==l&&o.value.length&&i(document.body).trigger("update_checkout"),o.value=""}))}),i("#billing_dic, #billing_dic_dph").donetyping(function(){var l=i("#billing_country").val();"SK"!=l&&"billing_dic"==this.id&&i(document.body).trigger("update_checkout"),"SK"==l&&"billing_dic_dph"==this.id&&i(document.body).trigger("update_checkout")},750)})}(jQuery);
\ No newline at end of file
+"use strict";!function(i){var l="";function e(){var e;switch(woolab.ares_fill&&(i(".woolab-ic-dic-tip").remove(),c()),i("#billing_ic_field").slideDown(),i("#billing_dic_field").slideDown(),i("#billing_country").val()){case"SK":i("#billing_dic_dph_field").slideDown(),i("#billing_dic_field > label").addClass("woolab-ic-dic-required");break;case"CZ":i("#billing_dic_dph_field").slideUp(),i("#billing_dic_field > label").removeClass("woolab-ic-dic-required");break;default:i("#billing_dic_dph_field").slideUp(),i("#billing_dic_field > label").removeClass("woolab-ic-dic-required")}woolab.ares_check&&(t(e=i("#billing_ic")),i(document.body).on("focusin","#billing_ic",function(){l=e.val()}),e.donetyping(function(){(e=i("#billing_ic")).val()!==l&&t(e)},500)),i("#billing_company_field").slideDown()}function o(){var l="SK"==i("#billing_country").val()?"billing_dic_dph":"billing_dic";i("#billing_company, #billing_ic, #billing_dic, #billing_dic_dph").each(function(e,o){o.getAttribute("data-value")&&(o.value=o.getAttribute("data-value"),o.id==l&&o.value.length&&i(document.body).trigger("update_checkout"))})}function n(i){i.removeClass("kbnt-wrong").removeClass("woocommerce-invalid")}function a(i){i.addClass("kbnt-wrong").addClass("woocommerce-invalid").removeClass("woocommerce-validated")}function c(){i("#billing_company").removeAttr("readonly"),i("#billing_dic").removeAttr("readonly"),i("#billing_postcode").removeAttr("readonly"),i("#billing_city").removeAttr("readonly"),i("#billing_address_1").removeAttr("readonly")}function t(e){if("CZ"===i("#billing_country").val()){var o=e.val();if(o!==l){var t=i("#billing_ic_field"),r=''+woolab.l18n_not_valid+"";i(".woolab-ic-dic-tip").remove(),n(t),t.removeClass("kbnt-ok").removeClass("woocommerce-validated"),7!=o.length&&8!=o.length||null==o.match(/^[0-9]+$/)?(c(),o.length>0?a(t):n(t)):i.ajax({url:woolab.ajaxurl,data:{action:"ajaxAres",ico:o},beforeSend:function(){t.addClass("kbnt-validating"),t.append(''+woolab.l18n_validating+"")},success:function(l){if(t.removeClass("kbnt-validating"),l)if(0==(l=JSON.parse(l)).error){if(i(".woolab-ic-dic-tip").remove(),t.addClass("kbnt-ok").addClass("woocommerce-validated").removeClass("woocommerce-invalid"),woolab.ares_fill){if(i("#billing_same_as_shipping")&&i("#billing_same_as_shipping").is(":checked")&&window.CollapsibleBlock){document.querySelector("#billing_same_as_shipping").checked=!1;var e=document.querySelector("#woocommerce-billing-fields__field-wrapper");e&&CollapsibleBlock.expand(e);var o=document.querySelector("#fc-expansible-form-section__toggle-plus--billing_company"),n=document.querySelector("#fc-expansible-form-section__content--billing_company");o&&CollapsibleBlock.collapse(o),n&&CollapsibleBlock.expand(n);var a=document.querySelector("#fc-expansible-form-section__toggle-plus--billing_dic"),s=document.querySelector("#fc-expansible-form-section__content--billing_dic");a&&CollapsibleBlock.collapse(a),s&&CollapsibleBlock.expand(s)}i("#billing_company").val(l.spolecnost).attr("readonly",!0),i("#billing_dic").val(l.dic).attr("readonly",!0),i("#billing_address_1").val(l.adresa).attr("readonly",!0),i("#billing_postcode").val(l.psc).attr("readonly",!0),i("#billing_city").val(l.mesto).attr("readonly",!0),t.append(''+woolab.l18n_ok+""),i("body").trigger("update_checkout")}}else d(t),i(".woolab-ic-dic-tip").length>0&&i(".woolab-ic-dic-tip").remove(),c(),l.internal_error&&woolab.ignore_check_fail||t.append(''+l.error+"");else d(t),0==i(".woolab-ic-dic-tip").length&&(c(),t.append(r))},error:function(l){0==i(".woolab-ic-dic-tip").length&&(e.val(""),d(t),t.append(''+woolab.l18n_error+""))}}),l=e.val()}}}function d(l){woolab.ares_fill&&(woolab.ignore_check_fail||(i("#billing_company").val(""),i("#billing_dic").val(""),i("#billing_postcode").val(""),i("#billing_city").val(""),i("#billing_address_1").val("")),c()),a(l)}i.fn.extend({donetyping:function(l,e){e=e||1e3;var o,n=function(i){o&&(o=null,l.call(i))};return this.each(function(l,a){var c=i(a);c.is(":input")&&i(document.body).on("keyup keypress paste",a,function(i){"keyup"==i.type&&8!=i.keyCode||(o&&clearTimeout(o),o=setTimeout(function(){n(a)},e))}),c.is(":input")&&i(document.body).on("blur",a,function(){n(a)})})}}),i(document).ready(function(){i(document.body).on("input",".woolab-ic-dic-no_spaces input",function(){i(this).val(function(i,l){return l.replace(/\s+/g,"")})});var l=i("#billing_iscomp");l.length&&!l.prop("checked")||(o(),e()),i(document.body).on("change","#billing_country, #billing_iscomp",function(){var l,n=i("#billing_iscomp");!n.length||n.prop("checked")?(o(),e()):(i(".woolab-ic-dic-toggle").slideUp(),l="SK"==i("#billing_country").val()?"billing_dic_dph":"billing_dic",i("#billing_company, #billing_ic, #billing_dic, #billing_dic_dph").each(function(e,o){o.setAttribute("data-value",o.value),o.id==l&&o.value.length&&i(document.body).trigger("update_checkout"),o.value=""}))}),i("#billing_dic, #billing_dic_dph").donetyping(function(){var l=i("#billing_country").val();"SK"!=l&&"billing_dic"==this.id&&i(document.body).trigger("update_checkout"),"SK"==l&&"billing_dic_dph"==this.id&&i(document.body).trigger("update_checkout")},750)})}(jQuery);
\ No newline at end of file
diff --git a/includes/ares.php b/includes/ares.php
index 3403cac..2ed1859 100644
--- a/includes/ares.php
+++ b/includes/ares.php
@@ -32,7 +32,7 @@ function woolab_icdic_ares( $ico = '' ) {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
- if ( $status_code == 200 && $data ) {
+ if ( $status_code === 200 && $data ) {
$return = array( 'error' => false );
$return['spolecnost'] = $data->obchodniJmeno ?? '';
@@ -49,14 +49,20 @@ function woolab_icdic_ares( $ico = '' ) {
$return['psc'] = $data->sidlo->psc;
$return['mesto'] = $data->sidlo->nazevMestskehoObvodu ?? $data->sidlo->nazevObce;
- } elseif ( $status_code == 404 ) {
+ } elseif ( $status_code === 404 ) {
$return = array( 'error' => __('Entity doesn\'t exist in ARES.', 'woolab-ic-dic'));
} else {
- $return = array( 'error' => __('ARES is not responding', 'woolab-ic-dic'));
+ $return = array(
+ 'error' => __('ARES is not responding.', 'woolab-ic-dic'),
+ 'internal_error' => true,
+ );
}
} else {
- $return = array( 'error' => __('An error occured while connecting to ARES, try it again later.', 'woolab-ic-dic'));
+ $return = array(
+ 'error' => __('An error occured while connecting to ARES, try it again later.', 'woolab-ic-dic'),
+ 'internal_error' => true,
+ );
}
return $return;
diff --git a/includes/filters-actions.php b/includes/filters-actions.php
index 32c495c..f6734ed 100644
--- a/includes/filters-actions.php
+++ b/includes/filters-actions.php
@@ -4,6 +4,7 @@
use KybernautIcDicDeps\Ibericode\Vat\Countries;
use KybernautIcDicDeps\Ibericode\Vat\Validator;
+use KybernautIcDicDeps\Ibericode\Vat\Vies\ViesException;
if ( ! defined( 'WPINC' ) ) {
die;
@@ -155,7 +156,12 @@ function woolab_icdic_checkout_field_process() {
return false;
}
- $country = $_POST['billing_country'];
+ $country = $_POST['billing_country'];
+ $ignore_vat_check_fail = woolab_icdic_ignore_check_fail();
+
+ // Flag to check if VAT check fail was ignored.
+ // The information will be saved in the order meta in woocommerce_new_order hook.
+ $vat_check_fail_ignored = false;
// BUSINESS ID
if ( isset( $_POST['billing_ic'] ) && $_POST['billing_ic'] ) {
@@ -175,7 +181,13 @@ function woolab_icdic_checkout_field_process() {
$ares = woolab_icdic_ares( $ico );
if ( $ares ) {
if ( $ares['error'] ) {
- wc_add_notice( __( 'Enter a valid Business ID', 'woolab-ic-dic' ) . ' ' . $ares['error'], 'error' );
+ $is_internal_error = ( ! empty( $ares['internal_error'] ) );
+
+ if ( $is_internal_error && $ignore_vat_check_fail ) {
+ $vat_check_fail_ignored = true;
+ } else {
+ wc_add_notice( __( 'Enter a valid Business ID', 'woolab-ic-dic' ) . ' ' . $ares['error'], 'error' );
+ }
} elseif ( woolab_icdic_ares_fill() ) {
if ( isset( $_POST['billing_dic'] ) && wc_clean( wp_unslash($_POST['billing_dic'])) != $ares['dic'] ) {
$missing_fields[] = __( 'Business ID', 'woocommerce' );
@@ -197,7 +209,11 @@ function woolab_icdic_checkout_field_process() {
}
}
} else {
- wc_add_notice( __( 'Unexpected error occurred. Try it again.', 'woolab-ic-dic' ), 'error' );
+ if ( $ignore_vat_check_fail ) {
+ $vat_check_fail_ignored = true;
+ } else {
+ wc_add_notice( __( 'Unexpected error occurred. Try it again.', 'woolab-ic-dic' ), 'error' );
+ }
}
// ARES Check Disabled
@@ -247,8 +263,18 @@ function woolab_icdic_checkout_field_process() {
wc_add_notice( __( 'VAT number has not correct format', 'woolab-ic-dic' ), 'error' );
}
- if ( ! $validator->validateVatNumber( $dic )) {
- wc_add_notice( __( 'Enter a valid VAT number', 'woolab-ic-dic' ), 'error' );
+ try {
+ $vat_number_valid = $validator->validateVatNumber( $dic );
+
+ if ( ! $vat_number_valid ) {
+ wc_add_notice( __( 'Enter a valid VAT number', 'woolab-ic-dic' ), 'error' );
+ }
+ } catch ( ViesException $exception ) {
+ if ( $ignore_vat_check_fail ) {
+ $vat_check_fail_ignored = true;
+ } else {
+ wc_add_notice( __( 'Could not validate VAT number.', 'woolab-ic-dic' ), 'error' );
+ }
}
// Validate CZ and SK mathematicaly
@@ -299,8 +325,18 @@ function woolab_icdic_checkout_field_process() {
$validator = new Validator();
- if ( ! $validator->validateVatNumber( $dic_dph )) {
- wc_add_notice( _x( 'Enter a valid VAT number', 'IC DPH', 'woolab-ic-dic' ), 'error' );
+ try {
+ $vat_number_valid = $validator->validateVatNumber( $dic_dph );
+
+ if ( ! $vat_number_valid ) {
+ wc_add_notice( _x( 'Enter a valid VAT number', 'IC DPH', 'woolab-ic-dic' ), 'error' );
+ }
+ } catch ( ViesException $exception ) {
+ if ( $ignore_vat_check_fail ) {
+ $vat_check_fail_ignored = true;
+ } else {
+ wc_add_notice( __( 'Could not validate VAT number.', 'woolab-ic-dic' ), 'error' );
+ }
}
} else {
@@ -320,6 +356,9 @@ function woolab_icdic_checkout_field_process() {
}
}
+ // Set flag about Business ID or VAT number check fails.
+ WC()->session->set( 'woolab_icdic_vat_check_fail_ignored', $vat_check_fail_ignored );
+
}
// My address formatted
@@ -419,20 +458,29 @@ function woolab_icdic_set_vat_exempt_for_customer() {
return;
}
- $vat_num = null;
- $wc_countries = new WC_Countries();
- $base_country = $wc_countries->get_base_country();
- $base_country = apply_filters( 'woolab_icdic_base_country', $base_country );
- $is_vat_exempt = false;
+ $vat_num = null;
+ $wc_countries = new WC_Countries();
+ $base_country = $wc_countries->get_base_country();
+ $base_country = apply_filters( 'woolab_icdic_base_country', $base_country );
+ $ignore_vat_check_fail = woolab_icdic_ignore_check_fail();
+ $is_vat_exempt = false;
if (!empty($customer->get_meta('billing_country')) && $customer->get_meta('billing_country') !== $base_country) {
$vat_num = $customer->get_meta('billing_country') == 'SK' ? $customer->get_meta('billing_dic_dph') : $customer->get_meta('billing_dic');
}
if (!empty($vat_num)) {
- $validator = new Validator();
+ $validator = new Validator();
if ( $validator->validateVatNumberFormat( $vat_num ) ) {
- $is_vat_exempt = $validator->validateVatNumber( $vat_num );
+ try {
+ $is_vat_exempt = $validator->validateVatNumber( $vat_num );
+ } catch ( ViesException $exception ) {
+ if ( $ignore_vat_check_fail ) {
+ $is_vat_exempt = true;
+ } else {
+ throw $exception;
+ }
+ }
}
}
@@ -450,12 +498,13 @@ function woolab_icdic_validate_vat_exempt_for_company( $post_data ) {
$data = array();
wp_parse_str($post_data, $data);
- $wc_countries = new WC_Countries();
- $base_country = $wc_countries->get_base_country();
- $base_country = apply_filters( 'woolab_icdic_base_country', $base_country );
- $country = $data['billing_country'];
- $vat_countries = $wc_countries->get_european_union_countries('eu_vat');
- $is_eu_country = in_array($country, $vat_countries);
+ $wc_countries = new WC_Countries();
+ $base_country = $wc_countries->get_base_country();
+ $base_country = apply_filters( 'woolab_icdic_base_country', $base_country );
+ $country = $data['billing_country'];
+ $vat_countries = $wc_countries->get_european_union_countries('eu_vat');
+ $is_eu_country = in_array($country, $vat_countries);
+ $ignore_vat_check_fail = woolab_icdic_ignore_check_fail();
if ($country === $base_country || !$is_eu_country) {
// Skip check if company's billing country is the same as store's country or if company's billing country is not EU VAT country.
@@ -469,7 +518,15 @@ function woolab_icdic_validate_vat_exempt_for_company( $post_data ) {
$is_vat_exempt = false;
if ( $validator->validateVatNumberFormat( $vat_num ) ) {
- $is_vat_exempt = $validator->validateVatNumber( $vat_num );
+ try {
+ $is_vat_exempt = $validator->validateVatNumber( $vat_num );
+ } catch ( ViesException $exception ) {
+ if ( $ignore_vat_check_fail ) {
+ $is_vat_exempt = true;
+ } else {
+ throw $exception;
+ }
+ }
}
$is_vat_exempt = apply_filters( 'woolab_icdic_vat_exempt_company', $is_vat_exempt, $data );
@@ -480,6 +537,27 @@ function woolab_icdic_validate_vat_exempt_for_company( $post_data ) {
}
}
+/**
+ * Saves order metadata on WooCommerce checkout.
+ * @param int $order_id
+ */
+function woolab_icdic_save_order_metadata( $order_id ) {
+ $order = wc_get_order( $order_id );
+
+ if ( ! $order instanceof WC_Order ) {
+ return;
+ }
+
+ $vat_check_fail_ignored = WC()->session->get( 'woolab_icdic_vat_check_fail_ignored' );
+
+ $order->update_meta_data(
+ 'woolab_icdic_vat_check_fail_ignored',
+ $vat_check_fail_ignored ? 'yes' : 'no'
+ );
+
+ $order->save_meta_data();
+}
+
// admin
function woolab_icdic_customer_meta_fields($fields) {
$fields['billing']['fields'] += array(
@@ -632,6 +710,132 @@ function woolab_icdic_process_shop_order ( $post_id, $post ) {
}
+/**
+ * Show notice about Business ID or VAT number check error
+ * below order number on orders table view.
+ * @param string $column Column ID.
+ * @param int $post_id Post ID.
+ */
+function woolab_icdic_show_check_failed_notice_on_orders_table( $column, $post_id ) {
+ if ( $column !== 'order_number' ) {
+ return;
+ }
+
+ $order = wc_get_order( $post_id );
+ $check_failed = ( $order instanceof WC_Order )
+ ? ( $order->get_meta( 'woolab_icdic_vat_check_fail_ignored' ) === 'yes' )
+ : false;
+
+ if ( ! $check_failed ) {
+ return;
+ }
+
+ ?>
+
+
+
+
+
+
+ get_meta( 'woolab_icdic_vat_check_fail_ignored' ) === 'yes' )
+ : false;
+
+ if ( ! $check_failed ) {
+ return;
+ }
+
+ ?>
+
+
+
+
+
+
+ get_meta( 'woolab_icdic_vat_check_fail_ignored' ) === 'yes' );
+
+ if ( ! $check_failed ) {
+ return;
+ }
+
+ ?>
+
+
+
+
+
+
+
+
+
+