Advertisement
businessdad

WooCommerce - Make VAT number optional if cart total is zero

Jan 26th, 2017
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. /**
  2.  * Customisation for the WooCommerce EU VAT Assistant.
  3.  * Makes the VAT Number optional when the cart total is zero, and required when the total is
  4.  * greater than zero. For more details, please refer to the original article:
  5.  * https://aelia.co/2017/01/27/woocommerce-eu-vat-assistant-vat-number-optional-cart-total-zero/
  6.  *
  7.  * Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
  8.  *
  9.  * @author Aelia
  10.  * @link https://aelia.co/2017/01/27/woocommerce-eu-vat-assistant-vat-number-optional-cart-total-zero/
  11.  * @license GPLv3
  12.  */
  13.  
  14. /* This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17.  *
  18.  * We (Aelia) won't be able to provide free assistance or support
  19.  * for this code, nor can we be held responsible for any issue,
  20.  * downtime or damage caused by its implementation.
  21.  */
  22.  
  23. /**
  24.  * Adds the cart total to the fragments returned as a response to the Ajax
  25.  * requests on the checkout page.
  26.  *
  27.  * @param array fragments The fragments returned as a response.
  28.  * @return array
  29.  */
  30. add_filter('woocommerce_update_order_review_fragments', function($fragments) {
  31.   $fragments['_cart_total'] = WC()->cart->total;
  32.   return $fragments;
  33. });
  34.  
  35. /**
  36.  * Adds a script to the checkout page, to make the VAT number required or not
  37.  * required, depending on the cart total.
  38.  */
  39. add_action('wp_footer', function() {
  40.   // We need to render our script only on the checkout page
  41.   if(!is_checkout()) {
  42.     return;
  43.   }
  44.   ?>
  45.   <script>
  46.   jQuery(document).ready(function($) {
  47.     // Run the script every time the checkout form is updated. This will
  48.     // allow us to check if the total changed
  49.     $(document.body).on('updated_checkout', function(ev, data) {
  50.       if(!data['fragments'] || !data['fragments'].hasOwnProperty('_cart_total')) {
  51.         return;
  52.       }
  53.  
  54.       var cart_total = parseFloat(data['fragments']['_cart_total']);
  55.       var vat_number_required = (cart_total > 0);
  56.  
  57.       var $eu_vat_number = $('#woocommerce_eu_vat_number');
  58.       $eu_vat_number.toggle(vat_number_required);
  59.       $eu_vat_number.find('.form-row').toggleClass('validate-required', vat_number_required);
  60.     });
  61.   })
  62.   </script>
  63.   <?php
  64. });
  65.  
  66. /**
  67.  * Sets the VAT number field as "not required" when the cart total is zero (or
  68.  * less; which should never happen, but better to cover that case).
  69.  *
  70.  * @param bool is_vat_number_required Indicates if the VAT number is required.
  71.  * @param string country The billing country selected at checkout.
  72.  * @return bool
  73.  */
  74. add_filter('wc_aelia_euva_order_is_eu_vat_number_required', function($is_vat_number_required, $country) {
  75.   // Make VAT number "not required" if the cart total is zero
  76.   if(WC()->cart->total <= 0) {
  77.     $is_vat_number_required = false;
  78.   }
  79.    
  80.   return $is_vat_number_required;
  81. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement