• Resolved kontur

    (@kontur)


    Hey,

    from what I’ve been testing this plugin it seems very good indeed. Is there a way to interact with the ajax result the plugin receives upon checking the VAT. E.g. I can see the ajax response in the network inspector, and would like to react to certain types of responses with showing my users additional information.

    Is this somehow possible and documented?

Viewing 8 replies - 1 through 8 (of 8 total)
  • SYNC4489

    (@sync4489)

    Very nice question!
    I would like to know too how to interact with VIES responses, in particular showing a notice if the VIES can’t be reached…

    Plugin Author Diego

    (@daigo75)

    At the moment, there isn’t an event triggered from the Ajax call. That call was added mostly for cosmetic reasons, to highlight the status VAT number (valid/not valid), and wasn’t really designed to be used by 3rd parties.

    I will add a note to see if we can add some event that you can intercept to read the result of the request. In the meantime, if you need to get the result of the validation, you can perform the same Ajax request yourself. Here’s an example how to do so:

    
    <script type="text/javascript">
    var vat_country = jQuery('#billing_country).val();
    var vat_number = jQuery('#var_number).val();
    var ajax_url = "<?php echo admin_url('admin-ajax.php', 'relative');?>";
    var ajax_args = {
      'action': 'validate_eu_vat_number',
      'country': vat_country,
      'vat_number': vat_number
    };
    jQuery.get(ajax_url, ajax_args, function(response) {
      // Read and manipulate the response here
      console.log(response);
    });
    </script>
    
    Plugin Author Diego

    (@daigo75)

    Good news! We added an event in version 1.7.15.171106 that is triggered when the VAT validation Ajax call is completed, so you won’t have to repeat the call yourself. You can write a handler as follows:

    
    jQuery(document).on('wc_aelia_euva_eu_vat_number_validation_complete', function(event, response) {
      // Print out the response object
      console.log(response);
      // Do what you need with the response
    });
    

    Please note that the call returns the raw response object returned by the call. If you get a “false”, then it means that the validation could not be performed, or that the VAT number is not valid.

    SYNC4489

    (@sync4489)

    Hello Diego,

    thanks for your help

    so if I understand correctly in the response there is no difference between an invalid number and a busy VIES server?

    Plugin Author Diego

    (@daigo75)

    The response is the raw data returned by the validation class. A value of false indicates that no response was returned. If the server did reply, the response contains the result of the validation in respoonse['valid'].

    From the plugin code:

    
    $validation_error = $validation_result['errors'][0];
    if(!empty($validation_error)) {
      // If server was busy, we may have to accept the VAT number as valid,
      // depending on plugin settings
      if(strcasecmp($validation_error, 'SERVER_BUSY') == 0) {
        $validation_result['valid'] = self::settings()->get(Settings::FIELD_ACCEPT_VAT_NUMBER_WHEN_VALIDATION_SERVER_BUSY, true);
      }
    }
    

    The $validation_result is what is returned as a response to the Ajax call. If you would like to customise the data returned by the validation, you can do so with a filter for hook wc_aelia_euva_eu_vat_number_raw_validation_result. The validation result is an array, you can add any information you like to it. Just make sure that you don’t change the data that it already contains, as the EU VAT Assistant is expecting it as it is.

    SYNC4489

    (@sync4489)

    Great, thanks again for your time! I’ll start playing with it this afternoon ??

    Thread Starter kontur

    (@kontur)

    Superb! Thank you for the quick feedback and the ever quicker implementation. Using the Ajax response now in my project to give the user more detailed feedback.

    Thread Starter kontur

    (@kontur)

    Since there is various different interactions triggering a VAT recheck I was wondering if there is a similar event to wc_aelia_euva_eu_vat_number_validation_complete for when the VAT check is initiated / triggered?

    If not, that would be great to have as well, so one could more concisely add a loading spinner until the request completes.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to interact with the ajax result’ is closed to new replies.