View difference between Paste ID: 0cAd8BPs and SfXGuVQd
SHOW: | | - or go back to the newest paste.
1
/**
2-
 * Aelia EU VAT Assistant for WooCommerce - Validate VAT numbers via code.
2+
 * Aelia EU VAT Assistant for WooCommerce - Add an order note when a valid VAT number is stored with an order.
3-
 * This code shows how to use the EU VAT Assistant to call the VIES service to validate a EU VAT number.
3+
 * NOTE
4
 * This code runs every time the VAT evidence is saved against an order. It doesn't check if the order note was
5-
 * This code is free of charge and there is no warranty for it, to the extent permitted by applicable law.
5+
 * already added with the same VAT number. Using this code with the example at https://pastebin.com/Pa0LLiLh could
6
 * result in the note being added multiple times.
7
 *
8
 * DISCLAIMER
9
 * Because this code is free of charge, there is no warranty for it, to the extent permitted by applicable law.
10
 * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
11
 * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
12
 * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
13-
 * of our paid consultation services: https://aelia.co/contact
13+
14
 *
15-
15+
16-
// Basic validation of a VAT number
16+
17-
$vat_number_valid = apply_filters('wc_aelia_eu_vat_assistant_validate_vat_number', $vat_number_valid, $country, $vat_number);
17+
 * of our paid consultation services: https://aelia.co/hire_us
18-
18+
19-
// Possible responses
19+
add_filter('wc_aelia_eu_vat_assistant_store_vat_evidence',  function($vat_evidence, $order) {
20-
const VAT_NUMBER_VALIDATION_NO_NUMBER = 'no-number';
20+
  // Add an order note with the VAT number when the number is present and valid
21-
const VAT_NUMBER_VALIDATION_VALID = 'valid';
21+
  if(!empty($vat_evidence['exemption']['vat_number']) &&
22-
const VAT_NUMBER_VALIDATION_NOT_VALID = 'not-valid';
22+
     !empty($vat_evidence['exemption']['vat_number_validated']) &&
23-
const VAT_NUMBER_VALIDATION_NON_EU = 'non-eu';
23+
     ($vat_evidence['exemption']['vat_number_validated'] === 'valid')) {
24-
const VAT_NUMBER_COULD_NOT_BE_VALIDATED = 'could-not-be-validated';
24+
    $order->add_order_note('VAT Number: ' . $vat_evidence['exemption']['vat_number']);
25-
const VAT_NUMBER_ENTERED_MANUALLY_NOT_VALIDATED = 'entered-manually-not-validated';
25+
  }
26-
26+
  // Return the VAT evidence as is, we don't need to modify it
27-
// Validation of a VAT number, returning the raw response. The response will
27+
  return $vat_evidence;
28-
// be an array with the data returned by the VIES service. You will have to parse
28+
}, 99, 2);