• Hi!

    There is an extension on the website, which is supposed to make the VAT value zero after entering the tax number in the case of a company purchase. However, it happens sometimes that the VAT is left in, this is not related to whether the user is registered or not and I have not managed to reproduce the error yet, but unfortunately I have received orders… The coding of the extension is as follows:

    <?php
    /*
    Plugin Name: Zero ado
    Plugin URI: http://yourwebsite.com/
    Description: Validates VAT numbers during checkout and applies a zero tax rate if the VAT number is valid.
    Version: 1.0
    Author: Your Name
    Author URI: http://yourwebsite.com/
    */
    
    // Enqueueing the javascript file
    function zero_ado_scripts() {
        wp_enqueue_script('zero-ado-js', plugin_dir_url(__FILE__) . 'zero-ado.js', array('jquery'), '1.0', true);
        wp_localize_script('zero-ado-js', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
    }
    add_action('wp_enqueue_scripts', 'zero_ado_scripts');
    
    // VAT number validation function
    function validate_vat_number($vat) {
        return preg_match('/^\d{8}-\d{1}-\d{2}$/', $vat);
    }
    
    // AJAX handler to validate VAT and apply zero rate
    function check_vat_number_ajax_handler() {
        if (validate_vat_number($_POST['billing_tax_number'])) {
            WC()->customer->set_is_vat_exempt(true);
            echo json_encode(['success' => true]);
        } else {
            WC()->customer->set_is_vat_exempt(false);
            echo json_encode(['success' => false, 'error' => 'Nem megfelelő adószám formátum!']);
        }
        
        WC()->cart->calculate_totals();
        wp_die();
    }
    add_action('wp_ajax_check_vat_number', 'check_vat_number_ajax_handler');
    add_action('wp_ajax_nopriv_check_vat_number', 'check_vat_number_ajax_handler');
    ?>
    jQuery(function($) {
        var vatValidated = true; // Default to true
    
        function setLoader(loading) {
            // ... (maradhat változatlan)
        }
    
        function validateVATNumber() {
            var vatNumber = $('#billing_tax_number').val();
            var companyName = $('#billing_company').val(); // Get the company name
    
            // Check if the company name is empty
            if (companyName.trim() === '') {
                vatValidated = true; // No VAT validation required if the company name is empty
                $('button[name="woocommerce_checkout_place_order"]').prop('disabled', false); // Enable place order button
                return;
            }
    
            setLoader(true); // Show loading
    
            $.ajax({
                url: ajax_object.ajax_url,
                method: 'POST',
                dataType: 'json', // Expect JSON response from the server
                data: {
                    action: 'check_vat_number',
                    billing_tax_number: vatNumber
                },
                success: function(response) {
                    if(response.success) {
                        vatValidated = true; // VAT is valid
                    } else {
                        vatValidated = false; // VAT is invalid
                        alert('Nem megfelelő adószám formátum!');
                    }
                },
                error: function() {
                    vatValidated = false; // In case of AJAX error, consider VAT as invalid
                },
                complete: function() {
                    setLoader(false); // Hide loading
                    $('body').trigger('update_checkout');
                    // Re-check if the place order button should be enabled
                    $('button[name="woocommerce_checkout_place_order"]').prop('disabled', !vatValidated);
                }
            });
        }
    
        // Run VAT validation when the VAT number field is changed
        $('#billing_tax_number').on('change', function() {
            validateVATNumber();
        });
    
        // Check VAT validation status before placing the order
        $('form.checkout').on('checkout_place_order', function() {
            if (!vatValidated) {
                validateVATNumber(); // Re-validate VAT if not done
                return false; // Prevent the form from submitting
            }
            return true; // VAT is validated or not required, proceed with form submission
        });
    
        // Initially disable the place order button until VAT is validated
        $('button[name="woocommerce_checkout_place_order"]').prop('disabled', true);
    
        // Clear the VAT number field on page load
        $('#billing_tax_number').val('');
    });
    

    Thank You!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘custom plugin works’ is closed to new replies.