Posts

news icon

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

news icon

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

news icon

Advanced WooCommerce Reporting adds native support for multi-currency reports

One of our clients informed us that the popular Advanced WooCommerce Reporting plugin now supports multi-currency setups. Its authors introduced this feature in version 3.0 of their product, which was released at the beginning of October 2016.

This is a fantastic news, as it means that it will be possible to produce advanced reports, with a few clicks, and have a complete overview of how your shop is performing. Make sure that you have a look at the latest version of Advanced WooCommerce Reporting, you can find it on CodeCanyon.

It’s great to see more developers join our efforts to make WooCommerce and Easy Digital Downloads fully support multi-currency scenarios. We would like to thank Proword (the plugin’s authors), for recognising the importance and relevance of multi-currency shops, and for the work they’ve done in supporting merchants who run these shops. Thanks to them, tracking the performance of worldwide sales has become easier than ever!

The Aelia Team

news icon

Pressidium add native support for Aelia plugins in their caching layer

This week starts with a great news! We have just been informed that Pressidium, a high performance hosting company, can now support Aelia products natively. At a simple request, they can to configure their caching layer to work with all of the WooCommerce plugins we developed.

Thanks to this feature, their dynamic caching will be able to take into account customer’s location, currency and tax settings. Your site will display the correct information to all your visitors, as soon as they land on your site, at a lightning fast speed!

Sounds great? Yes, we think so!

How to get support for Aelia plugins on Pressidium

That couldn’t be easier: if you have your site hosted with Pressidium, simply contact their support team, and ask them to enable support for the Aelia plugin on their caching layer. That would be all!

In the unlikely event in which the support person is not familiar with the setup, you can simply forward them the link to our documentation: Aelia- How to add dynamic caching to your site. They will know what to do to get your site blazing fast.

 

We are sure that you are as excited as we are about this excellent news. Super fast, multi-currency sites are now at your fingertips!

The Aelia Team

news icon

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

news icon

WooCommerce and cache – Part 2: new Cache Handler plugin

Read Part 1: WooCommerce Currency Switcher and cache – Making them work together

Some months ago, we wrote about one of the most common issues faced by merchants who run highly dynamic websites, which include multi-currency, multi-pricing, geolocation features: stale content served by rigid caching systems.

In brief, there are quite a few caching systems designed with the assumption that the content of a site is the same for anyone. No matter who is opening a page, they serve the same information. This is correct for relatively static sites, such as blogs, which show the same articles to every member of the audience, or simple e-shops, where the prices are set, and are the same for every customer.

In a few words, many caching systems assume that, given a page on a site, the content of such page will always be the same, without exception. As we explained, this is incorrect when a site is highly dynamic. A shop that handles multiple currencies may be showing different prices to different visitors, or it might need to show a different tax rate (this is actually a requirement in many countries), on the exact same page.

With a static caching system, the result is that users may end up seeing the wrong content. This makes for a worse user experience, and can have an impact on conversion.

After an in-depth analysis, and several experiments, we came to a conclusion: issues caused by the caching system must be solved by the caching system. Based on this approach, we prepared an algorithm for dynamic caching that handles the needs of a multi-currency, multi-pricing, multi-language shop as it should, without compromising on the performance. This, in our opinion, is the correct way to address the issue.

The status of Dynamic Caching today

We keep contacting as many hosting providers as possible, explaining them how they should update their caching systems to bring them up to speed. Our objective is to make it clear that handling highly dynamic caching is a must, not just a “frill”.

Quite a few providers agreed with our approach, and allow their customers to customise the caching logic as required. Others, like the WP Engine team, showed interest in our solution, and are currently reviewing it, for future implementation. There are some who are still “lagging behind”, and there are a few, such as CloudFlare, who only offer dynamic caching on their most expensive plans.

Due to these limitations, imposed by an obsolete architecture, merchants have make a difficult decision:

  • Change hosting/service provider and move to one that handles caching as it should. This would make sense, but it’s not always possible. Besides, merchants might have invested a significant sum in current service.
  • Disable the caching system they are using, and for which they might have been paying a service fee.
  • Abandon the idea of a multi-currency, multi-language site, potentially risking to drive away audience.

While we still maintain that caching issues must be addressed on the caching layer, we wanted to find a solution that would help merchants, at least temporarily, while the service providers update their system.

Welcome our new plugin: WooCommerce Cache Handler

Thanks to our customers’ support, we are happy to announce the release of the WooCommerce Cache Handler plugin (currently in Beta stage). This new plugin can be used as a workaround with rigid caching systems, such as CloudFlare, SiteGround “Dynamic” Cache (which is actually static), as well as plugins that don’t support dynamic caching, such as W3 Total Cache.

Please note that our recommendation is still to consider switching to a more flexible solution, but this plugin will cover you until you are ready for that change.

How it works

The WooCommerce Cache Handler is simple to use. All you have to do after installing the plugin is go to WooCommerce Settings > Cache Handler and choose the handler you prefer.

WooCommerce Cache Handler - Configuration page

Configuring WooCommerce Cache Handler is extremely easy. Simply choose your favourite handler, clear the cache, and you are ready to go!

After that simple selection, the Cache Handler will support our Currency Switcher, Prices by Country, Tax Display by Country, and all our other WooCommerce plugins.

Currently, there are three options available.

1. Disabled

As the name implies, this disable all the features of the plugin. It can be useful for testing.

2. Redirect

This option is an almost exact equivalent of the “caching support” feature implemented by WooCommerce, which is enabled when the Default Customer Address is set to Geolocate (with page caching support).

The major difference from the standard feature is that our handler takes into account details such as the currency, customer’s country, customer’s state, customer’s tax exemption, and so on, ensuring that the correct content is served to customers based on these parameters. Like the original workaround from which it was derived, this handler appends a random string to URLs.

Benefits and drawbacks

+ The Redirect handler is based on the original workaround implemented by WooCommerce.
+ This solution works on a page level, thus it can produce the correct content with any configuration, or 3rd party plugins.
– The URLs look “ugly”, due to the string appended to them to work around the limitations of caching.

3. Ajax Loader

The Ajax Loader is an alternative to the Redirect, and it’s more elegant, as it doesn’t alter page URLs with ugly, random text. This handler loads all pricing elements via Ajax, when the page load is completed. The result is the following:

  1. A visitor connects to your site from the US. He would like to see USD.
  2. The rigid caching system has the page cached in EUR, and serves it to the customer.
  3. The Ajax Loader kicks in as soon as the page is loaded, requesting the updated prices from the server. After a brief moment, all elements that were displayed in EUR are changed to USD, as the visitor would expect.This update is very quick, and, at the moment, it processes the following standard elements :
    – Product prices
    – Currency selectors
    – Price filter widget
    – Custom prices displayed with the Currency Switcher shortcodes.

Benefits and drawbacks

+ The Ajax Loader is a more elegant solution than reloading the page after appending a random string to the URL.
+ Page load is faster, as there is no redirect.
+ URLs are not altered. There is no random text appended to them.
– The Ajax Loader can only process standard elements on the page. If 3rd party plugins add their own pricing elements, such addon prices, custom totals, etc, those won’t be updated. It will be up to the 3rd party plugins’ authors to “hook” into the Ajax Loader and refresh their elements via Ajax.

How to get the WooCommerce Cache Handler

The WooCommerce Cache Handler is available free of charge. Please feel free to download and try it, and see how it works for you. Whether you are using CloudFlare, SiteGround, WP Engine, Flywheel, or any other service with static caching, the Cache Handler got you covered!

Questions? Feedback?

The Cache Handler is still in Beta stage and that, as any free plugin, it’s not covered our free support. We would recommend to try it on a staging copy of your site, so that the live site won’t not affected by bugs that we eventually have to address.

We also would like to encourage you to share your feedback by contacting us. If you wish to contribute to the development of this plugin, you are more than welcome to do so!

Thanks for reading, and see you soon on aelia.co!

The Aelia Team