• Resolved urgul

    (@urgul)


    Hello,

    I have such a problem:

    I created a special php snippet that validates one of the fields added with the Checkout Field Editor for WooCommerce (Pro) plugin in the checkout form (WPFunnels plugin sees these fields in its checkout). In Poland, additional fields “company name” and NIP (VAT) are needed for invoicing, so I had to add them (and it is the NIP (VAT) field that validates my snippet).

    The snippet is presented as follows:

    add_filter('woocommerce_checkout_posted_data', 'sanitize_and_validate_billing_vat_number');

    function sanitize_and_validate_billing_vat_number($data)
    {

    // Checks that the checked checkbox field named “billing_vat_on” exists and has a value of true, and that the NIP (VAT) field is not empty
    if(isset($_POST['billing_vat_on']) && $_POST['billing_vat_on'] && !empty($data['billing_vat_number'])){

    $billing_vat_number = $data['billing_vat_number'];

    // Removes spaces and hyphens from the NIP (VAT) number
    $billing_vat_number = str_replace([' ', '-'], '', $billing_vat_number);

    // Checks if the NIP (VAT) number has 10 digits
    if (preg_match('/^[0-9]{10}$/', $billing_vat_number) == false) {
    wc_add_notice(__('<strong>Podany numer NIP jest niepoprawny.</strong> Wpisz polski numer NIP bez spacji i myślników.', 'woocommerce'), 'error');
    } else {
    // Calculates the checksum
    $weights = [6, 5, 7, 2, 3, 4, 5, 6, 7];
    $sum = 0;
    for ($i = 0; $i < 9; $i++) {
    $sum += $billing_vat_number[$i] * $weights[$i];
    }
    $control_number = $sum % 11;

    // Checks whether the checksum is correct
    if ($control_number != $billing_vat_number[9]) {
    wc_add_notice(__('<strong>Podany numer NIP jest niepoprawny.</strong> Wpisz polski numer NIP bez spacji i myślników.', 'woocommerce'), 'error');
    } else {
    // Saves the filtered value of the field “billing_vat_number”
    $data['billing_vat_number'] = $billing_vat_number;
    }
    }
    }
    return $data;
    }

    If the customer wants to receive an invoice, he checks the box “I want to receive an invoice” and 2 fields appear for completion: company name and NIP.

    I have a dedicated plug-in installed for invoicing (whose name is Woocommerce FakturaXL by WPDesk). This plugin automatically issues invoices when the order status changes to Completed.

    The problem is as follows: while on a standard Woocommerce checkout page with the Woocommerce FakturaXL plugin enabled, everything works as expected, the checkout used in a sales funnel created with the WPFunnels plugin does not. To be more precise – the script works and validates NIP (VAT) in the WPFunnels funnel only when I disable the Woocommerce FakturaXL plugin and stops validating when I enable it.

    Are you able to solve this problem?

    I created the validation to eliminate the problem with misspelled NIP (VAT) already at the checkout stage. Unfortunately, when I enable the FakturaXL plugin, this validation stops working in the WPFunnels sales funnel (which is very important for my sales activities), causing customers to write nonsense in the NIP field, which in turn prevents automatic issuance of documents.

    I am using the latest version of all plugins Woocommerce FakturaXL 1.7.0, Woocommerce 9.1.2, WPFUnnels 3.4.7

    I am very much asking for help.

    • This topic was modified 1 year, 9 months ago by urgul.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter urgul

    (@urgul)

    I discovered that this problem is not related to Woocommerce FakturaXL, but to the WPFunnels checkout widget (Elementor) itself.

    Explaining once again:

    I have created additional fields in Checkout Field Editor for WooCommerce (Pro) for my checkout purposes:

    1. checkbox – “I want to receive an invoice”
    2. and 2 more fields – “company name” and NIP (VAT), which appear only when “I want to receive an invoice” is checked, and hide when uchecked.

    The problem is that the first time I check the box “I want to receive an invoice” and enter an invalid NIP number, the code that validates its correctness works, but when I uncheck this checkbox and check it again, the validation no longer works (really strange behavior). This problem occurs regardless of whether the Woocommerce FakturaXL plugin is enabled or not, so it has nothing in common with it.

    I’ll add that after the WPFunnels checkout page loads, all I have to do is check the checkbox “I want to receive an invoice”, uncheck it and check it again and then enter the wrong NIP (VAT) and the validation no longer works.

    To sum up – after checking the checkbox “I want to receive an invoice” for the first time and entering the wrong NIP, the validation works, after unchecking the checkbox and checking it again the validation no longer works.

    I should add that in the standard Elementor checkout widget this problem does not occur – the snippet validates the correctness of the NIP (VAT) field always, no matter how many times I check or uncheck the checkbox “I want to receive an invoice”.

    So the problem is with WPFunnels.

    Plugin Support C S Sultan

    (@cssultan)

    Hi @urgul , thanks for explaining the situation.

    WPFunnels re-uses the default checkout of WooCommerce with some additional modifications. In this case, certain snippets may not work if not exclusively defined as per the right class names.

    I would request you to create a checkout page while keeping the snippet on and sharing it with us. Then we can observe what’s happening differently with the snippet. It would be even better if a staging site can be provided for us to look into this. (For that, please email us.)

    If we get to work on a similar environment, it will be quite easy for us to identify what could be causing the anomaly.

    Thanks.

    Thread Starter urgul

    (@urgul)

    Hi @cssultan, sorry for the delay – I was on vacation.

    I found a solution of this issue. I added this php snippet:

    add_filter( 'woocommerce_checkout_get_value', 'uncheck_billing_vat_on', 10, 2 );

    function uncheck_billing_vat_on( $value, $input ) {
    if ( $input === 'billing_vat_on' ) {
    return '';
    }
    return $value;
    }

    and changed the snippet with VAT validation:

    // Zapisujemy stan checkboxa w sesji
    add_action('woocommerce_checkout_process', 'save_vat_checkbox_state');
    function save_vat_checkbox_state() {
    WC()->session->set('billing_vat_on', isset($_POST['billing_vat_on']) ? true : false);
    }

    // Modyfikujemy funkcję walidującą NIP
    add_filter('woocommerce_checkout_posted_data', 'sanitize_and_validate_billing_vat_number');
    function sanitize_and_validate_billing_vat_number($data) {
    $billing_vat_on = WC()->session->get('billing_vat_on');

    if ($billing_vat_on) {
    $billing_vat_number = isset($data['billing_vat_number']) ? $data['billing_vat_number'] : '';

    // Jeśli pole NIP jest puste, nie dodajemy błędu
    if (empty($billing_vat_number)) {
    return $data;
    }

    // Usuwa spacje i myślniki z numeru NIP
    $billing_vat_number = str_replace([' ', '-'], '', $billing_vat_number);

    // Sprawdza, czy numer NIP ma 10 cyfr
    if (!preg_match('/^[0-9]{10}$/', $billing_vat_number)) {
    wc_add_notice(__('<strong>Podany numer NIP jest niepoprawny.</strong> Wpisz polski numer NIP bez spacji i myślników.', 'woocommerce'), 'error');
    } else {
    // Wylicza sumę kontrolną
    $weights = [6, 5, 7, 2, 3, 4, 5, 6, 7];
    $sum = 0;
    for ($i = 0; $i < 9; $i++) {
    $sum += $billing_vat_number[$i] * $weights[$i];
    }
    $control_number = $sum % 11;

    // Sprawdza, czy suma kontrolna jest poprawna
    if ($control_number != $billing_vat_number[9]) {
    wc_add_notice(__('<strong>Podany numer NIP jest niepoprawny.</strong> Wpisz polski numer NIP bez spacji i myślników.', 'woocommerce'), 'error');
    } else {
    // Zapisuje przefiltrowaną wartość pola "billing_vat_number"
    $data['billing_vat_number'] = $billing_vat_number;
    }
    }
    }
    return $data;
    }

    // Dodajemy akcję czyszczenia sesji po zakończeniu zamówienia
    add_action('woocommerce_thankyou', 'clear_vat_session');
    function clear_vat_session() {
    WC()->session->__unset('billing_vat_on');
    }

    add_action('wp_ajax_update_vat_checkbox', 'update_vat_checkbox_session');
    add_action('wp_ajax_nopriv_update_vat_checkbox', 'update_vat_checkbox_session');

    function update_vat_checkbox_session() {
    WC()->session->set('billing_vat_on', ($_POST['state'] === '1'));
    wp_die();
    }

    I don’t know if it’s the best solution, but it works for now.

    Would you like to get access to staging site to explore this?

    Plugin Support C S Sultan

    (@cssultan)

    Hi @urgul , if you found the solution, then that’s great. The code you used seems to be fine.

    If you face the issue again, then let us know and we will look into it at that time.

    No need to provide us with staging right now.

    Feel free to open more threads if you face any further issues.

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Issue with VAT Field Validation in WPFunnels checkout when using FakturaXL plug’ is closed to new replies.