Posts

Aelia - News badge

WooCommerce EU VAT Assistant – Make VAT number optional when cart total is zero

Recently, we have been asked for a customisation for the EU VAT Assistant, to cover the following scenario:

  • The VAT number should be required to complete a purchase, if a payment has to be made.
  • The VAT number should not be required if the checkout doesn’t require a payment (i.e. if the cart total is zero). This could be the case if the customer only adds free products to the cart.

The above scenario is outside the scope of the EU VAT Assistant. The purpose of the “VAT number required” option is to force only B2B transactions. By making the VAT number required, only verified businesses can complete a transaction. By design, this rule applies whether the transaction requires a payment or not.

If you need to make the VAT number required only when a payment is needed, you can easily do so with a customisation. Your custom code will have to cover two elements:

  1. The validation of the VAT number on the checkout page
    When the field is set as “required”, the customer cannot go ahead with the checkout until such field is filled. For this part, we will rely on JavaScript to show or hide the VAT number field dynamically, as well as making it required, or not required
  2. The validation of the VAT number during the checkout process
    For this part, we will use a simple filter to verify if the VAT number should be required during the final checkout phase.

Now we have a plan, let’s get started.

Step 1 – Expose the cart total to the JavaScript frontend

The cart total is not easily accessible via JavaScript, as it’s not stored in the data returned by the Ajax requests triggered on the checkout page. Luckily, there is a convenient  filter that will allow us to add that information, called woocommerce_update_order_review_fragments. Our filter will be the following.

/**
 * Adds the cart total to the fragments returned as a response to the Ajax
 * requests on the checkout page.
 *
 * @param array fragments The fragments returned as a response.
 * @return array
 */
add_filter('woocommerce_update_order_review_fragments', function($fragments) {
  $fragments['_cart_total'] = WC()->cart->total;
  return $fragments;
});

Done. Now, every time the checkout form changes, we will have the cart total handy.

Step 2 – Get the cart total from the fragments, via JavaScript

Now that we have the cart total exposed to the JavaScript on the checkout page, we can use it to show or hide the VAT number field, as well as change its “required” status. For that purpose, we just have to add a simple script to the page footer.

/**
 * Adds a script to the checkout page, to make the VAT number required or not
 * required, depending on the cart total.
 */
add_action('wp_footer', function() {
  // We need to render our script only on the checkout page
  if(!is_checkout()) {
    return;
  }
  ?>
  <script>
  jQuery(document).ready(function($) {
    // Run the script every time the checkout form is updated. This will
    // allow us to check if the total changed
    $(document.body).on('updated_checkout', function(ev, data) {
      if(!data['fragments'] || !data['fragments'].hasOwnProperty('_cart_total')) {
        return;
      }

      var cart_total = parseFloat(data['fragments']['_cart_total']);
      var vat_number_required = (cart_total > 0);

      var $eu_vat_number = $('#woocommerce_eu_vat_number');
      // Show the VAT number is the cart total is greater than zero,
      // hide it otherwise
      $eu_vat_number.toggle(vat_number_required);
      // Make the VAT number required only if the cart total is greater than zero
      $eu_vat_number.find('.form-row').toggleClass('validate-required', vat_number_required);
    });
  })
  </script>
  <?php
});

With this script, we covered the checkout page. The VAT number will appear automatically when the cart total is greater than zero, and disappear when it’s not. Customers will be able to checkout without entering a number, if no payment is needed.

Step 3 – Make the VAT number optional during the checkout process

This is the last step, to allow the checkout to complete when the cart total is zero and the customer did not enter a VAT number. This filter is very simple.

/**
 * Sets the VAT number field as "not required" when the cart total is zero (or
 * less; which should never happen, but better to cover that case).
 *
 * @param bool is_vat_number_required Indicates if the VAT number is required.
 * @param string country The billing country selected at checkout.
 * @return bool
 */
add_filter('wc_aelia_euva_order_is_eu_vat_number_required', function($is_vat_number_required, $country) {
  // Make VAT number "not required" if the cart total is zero
  if($is_vat_number_required && (WC()->cart->total <= 0)) {
    $is_vat_number_required = false;
  }
	
  return $is_vat_number_required;
}, 10, 2);

That’s it. We covered the checkout process as well. The result will be the following:

  • When the cart total is greater than zero, the VAT number will be required. It won’t be possible to complete the checkout without entering it.
  • When the cart total is zero, the VAT number will be hidden, and optional. Customers won’t need to enter it.

You can find the complete code here: WooCommerce – Make VAT number optional if cart total is zero (Pastebin).

Need help?

Should you need assistance adding this custom code to your site, or if you need it tailored to your needs, you can hire us on Codeable. We will analyse your specifications and send you an estimate for your customisation.

Thanks for reading, and for using our EU VAT Assistant. See you soon for the next WooCommerce Tips & Tricks!

The Aelia Team

Aelia - News badge

VIES Validation Service is down, affecting EU VAT number validation

We have been informed that the VIES service used by our EU VAT Assistant to validate VAT numbers is currently inaccessible. Here is a screenshot from the European Commission website, taken today, 23rd January, at 11:30 GMT:

EU Commission - Website unavailable (screenshot)

EU Commission – Website unavailable

This downtime will affect the validation operations performed by our plugin, which will be unable to validate VAT numbers. Unfortunately, this is outside of our control. We will have to wait until the service is back online, then the EU VAT Assistant will start working correctly again.

Apologies for the inconvenience on behalf of the team managing the EU website.

The Aelia Team

Aelia - News badge

Happy new year! Time to update VAT rate for Romania

Happy new year to you all! We hope you had some great Christmas time, and that you are ready for a great 2017. Considering how 2016 went, it can only get better!

The new year starts with a good news for our Romanian friends. As announced last year, the Romanian government reduced the standard VAT rate from 20% to 19%, from the 1st January 2017.

This means that your products will be slightly cheaper. It would be a good idea to update your tax settings as soon as possible.

How to update the tax rates

Updating tax rates is a simple operation:

  1. Go to WordPress Admin > WooCommerce > Settings > Tax.
  2. Click on the tax rate you would like to update (e.g. “Standard“), at the top of the page.
  3. Change the rate in the row with the country code “RO” to “19”.
  4. If needed, update the tax rate description.
  5. Save the changes.

Now all that’s left is double checking that all tax rates are correct. Our plugin updates the rates related to EU countries, therefore you will have to check the rates that refer to countries outside the European Union. If you don’t have any, then you’re done. WooCommerce will now use the new rates for orders placed from now on, and our plugin will collect the tax data automatically.

Again, best wishes for an incredible 2017 from the Aelia Team. May the new year be full of joy and, of course, business opportunities!

The Aelia Team

Aelia - News badge

EU VAT rate update – Greece

We have recently been informed that the standard VAT rate for Greece was changed on the 1st June 2016, from 23% to 24%. Some changes were made to reduced VAT rates as well.

We would recommend to take the opportunity to update the VAT rates configured in your system, to make sure that you are using the correct ones.

How to update the tax rates

Updating tax rates is a simple operation:

  1. Go to WordPress Admin > WooCommerce > Settings > Tax.
  2. Click on the tax rate you would like to update (e.g. “Standard“), at the top of the page.
  3. Change the rate in the row with the country code “GR” to “24”.
  4. Save the changes.

If you are using our EU VAT Assistant plugin, you can update all EU tax rates with a single click. Simply select the rate type at the bottom of the page and click on Update EU VAT Rates. Make sure that you select the appropriate rates (Standard or Reduced), the plugin will do the rest.

WooCommerce Tax Rates Settings - Screenshot

With our EU VAT Assistant you can update all VAT rates with a single click

Once the operation is completed, review the tax rates, to ensure that they are correct, and click on Save Changes. WooCommerce will now use the new rates.

Quick and easy! 🙂

The Aelia Team

Aelia - News badge

Remember to update your EU VAT rates!

If you didn’t do it already, it’s time to update the VAT rates on your system. From the 1st of January 2016, Romania cut its standard VAT rate from 24% to 20%. This quite good news, as the change will make your products cheaper to your Romanian customers, therefore we would recommend to update your tax settings as soon as possible.

How to update the tax rates

Updating tax rates is a simple operation:

  1. Go to WordPress Admin > WooCommerce > Settings > Tax.
  2. Click on the tax rate you would like to update (e.g. “Standard“), at the top of the page.
  3. Change the rate in the row with the country code “RO” to “20”.
  4. Save the changes.

If you are using our popular EU VAT Assistant, you can also refresh all EU tax rates with a single click. Simply select the rate type at the bottom of the page and click on Update EU VAT Rates.

WooCommerce Tax Rates Settings - Screenshot

With our EU VAT Assistant you can update all VAT rates with a single click

All tax rates will be updated automatically to their most recent value. No risk to forgetting any of them behind!

Now all that’s left is double checking that all tax rates are correct. Our plugin updates the rates related to EU countries, therefore you will have to review the rates that refer to countries outside the European Union. If you don’t have any, then you’re done. WooCommerce will now use the new rates for orders placed from now on, and our plugin will collect the tax data automatically.

It couldn’t be easier! 🙂

The Aelia Team

Aelia - Product update badge

EU VAT Assistant for WooCommerce updated to version 0.10.5.141231

EU VAT Assistant for WooCommerce – What’s new

EU VAT Assistant for WooCommerce has been updated to version 0.10.5.141231. This update brings the following improvements and fixes:

  • Added possibility to make the EU VAT Number field optional, required, required for EU countries or hidden. This feature will make it possible to accept only B2B or B2C sales.
  • Added exchange rates provider for Danish National Bank exhange rates feed.
  • Improved checks on of VAT validation responses. The new validation logic will prevent issues caused by corrupt cached responses.
  • Improved EU VAT by Country report. The report now loads VAT refunds dynamically.
  • Added recording, against each order, of the timestamp and provider of the VAT currency exchange rate used for reporting. These details will be useful in case of audit, to prove why a specific exchange rate was used.
  • Optimised loading of JavaScript parameters for Tax Settings admin pages.
  • Improved validation of VAT rates to be used on WooCommerce > Tax settings page.
  • Reorganised Admin user interface to make it tidier and more intuitive.
  • Fixed bug in VAT Number validation. The bug caused validation to fail when “odd” characters were returned by VIES service
  • Fixed incorrect reference to plugin class.
    Fixed bug in range calculation.
  • Fixed bug in handling of VIES response containing non-Latin UTF-8 characters
  • Fixed bug in handling of VAT rates for Isle of Man and Monaco

How to get the latest version

You can download the latest version of the EU VAT Assistant from the WordPress repository.