Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Customisation for the WooCommerce EU VAT Assistant.
- * Makes the VAT Number optional when the cart total is zero, and required when the total is
- * greater than zero. For more details, please refer to the original article:
- * https://aelia.co/2017/01/27/woocommerce-eu-vat-assistant-vat-number-optional-cart-total-zero/
- *
- * Need help customising the code for your need? Hire us on Codeable: http://bit.ly/codeable_aelia
- *
- * @author Aelia
- * @link https://aelia.co/2017/01/27/woocommerce-eu-vat-assistant-vat-number-optional-cart-total-zero/
- * @license GPLv3
- */
- /* This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * We (Aelia) won't be able to provide free assistance or support
- * for this code, nor can we be held responsible for any issue,
- * downtime or damage caused by its implementation.
- */
- /**
- * 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;
- });
- /**
- * 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');
- $eu_vat_number.toggle(vat_number_required);
- $eu_vat_number.find('.form-row').toggleClass('validate-required', vat_number_required);
- });
- })
- </script>
- <?php
- });
- /**
- * 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(WC()->cart->total <= 0) {
- $is_vat_number_required = false;
- }
- return $is_vat_number_required;
- }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement