Bedingtes Validierungsproblem für benutzerdefinierte Checkout-Felder in Woocommerce

Lesezeit: 3 Minuten

Benutzer-Avatar
Evakos

Ich habe ein Kontrollkästchen, das, wenn es aktiviert ist, ein Dropdown-Feld öffnet (Anforderungen). Wenn Sie versuchen, die Woocommerce-Bestellung zu senden, prüft es bedingt, ob das Feld Inhalt hat, wenn nicht, gibt es einen Fehler zurück.

Dies alles funktioniert, wenn das Anforderungsfeld eine Informationseingabe enthält, behandelt es es immer noch als inhaltslos und gibt den Fehler zurück.

Da dies die grundlegende Art und Weise ist, wie Formulare funktionieren sollten, verstehe ich nicht, warum ich ein solches Ergebnis erhalte. Hier ist der Code:

/**
 * Add a message field to the WC checkout
 */
add_action( 'woocommerce_before_checkout_form', 'custom_checkout_field' );

function custom_checkout_field( $checkout ) {

    echo '<div id="message"><h3>' . __( '<i class="fas fa-envelope"></i> Message' ) . '</h3><p style="margin: 0 0 8px;">Would you like to leave a message?</p>';

        woocommerce_form_field( 'checkbox', array(
            'type'  => 'checkbox',
            'class' => array( 'msg-checkbox' ),
            'label' => __( 'Yes' ),
        ), $checkout->get_value( 'checkbox' ) );

    woocommerce_form_field( 'requirements', array(
        'type'          => 'text',
        'class'         => array('msg'),
        'label'         => __('Please input your message.'),
        'placeholder'   => __(''),
        ), $checkout->get_value( 'requirements' ));

    echo '</div>';

}

/**
 * Process checkout with additional custom field
 */
add_action('woocommerce_checkout_process', 'custom_checkout_field_process');

function custom_checkout_field_process() {

    // Check if set, if not add an error.

    if(isset($_POST['checkbox']) && ( empty($_POST['requirements'])));

        wc_add_notice( __( 'Please let us know what you would like to do' ), 'error' );
}

Ich hoffe das ergibt Sinn. Grundsätzlich funktioniert es bis zu einem gewissen Punkt, aber die Validierung für das Eingabefeld funktioniert nicht, wenn es Inhalt hat.

Vielen Dank.

  • Vielen Dank. Du hast Recht. Es war nicht klar.

    – Evakos

    27. Februar 2018 um 18:01 Uhr

Benutzer-Avatar
LoicTheAztec

Aktualisieren: Codeverbesserungen und Hinzufügen einer Show/Hide-Funktion mit jQuery

Das einzige Problem kommt von dem Hook, den Sie für Ihre benutzerdefinierten Checkout-Felder verwenden. Es gibt diese Felder außerhalb des Checkout-Formulars aus, sodass die Werte nie übermittelt werden und dann immer leer sind.

Stattdessen werden Sie verwenden woocommerce_checkout_before_customer_details Action-Hook für Ihre erste Hook-Funktion, die diese Felder ausgibt im Checkout-Formular diesmal:

// Output custom checkout fields
add_action( 'woocommerce_checkout_before_customer_details', 'custom_checkout_field_before_billing' );
function custom_checkout_field_before_billing() {
    $domain = 'woocommerce';

    // Hide the message text field on load
    ?>
    <style>p#requirements_field{display:none;}</style>

    <div id="message">
    <h3><i class="fa fa-envelope"></i><?php _e( 'Message', 'woocommerce' ); ?></h3>
    <?php

    woocommerce_form_field( 'checkbox_msg', array(
        'type'  => 'checkbox',
        'class' => array( 'msg-checkbox' ),
        'label' => __( 'Would you like to leave a message?', 'woocommerce' ),
    ), WC()->checkout->get_value( 'cb_msg' ));

    woocommerce_form_field( 'requirements', array(
        'type'              => 'text',
        'class'             => array('msg t_msg'),
        'label'             => __('Please input your message.'),
        'placeholder'       => __(''),
    ), WC()->checkout->get_value( 'requirements' ));

    echo '</div>';

    // jQuery: show hide the text requirement field
    ?><script>
    jQuery(document).ready(function($) {
        var a="#requirements_field";
        $('input#checkbox_msg').change( function(){
            if( $(this).is(':checked') )
                $(a).show();
            else
                $(a).hide();
        });
    });
    </script><?php
}

// Process checkout with additional custom field
add_action('woocommerce_after_checkout_validation', 'custom_checkout_field_validation_process', 20, 2 );
function custom_checkout_field_validation_process( $data, $errors ) {

    // Check if set, if not add an error.
    if( isset($_POST['checkbox_msg']) && empty($_POST['requirements']) )
        $errors->add( 'requirements', __( "Please let us know what you would like to do filling up the message field.", "woocommerce" ) );
}

Der Code wird in die function.php-Datei Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt.
Getestet und funktioniert.

Geben Sie hier die Bildbeschreibung ein

  • Brillant. Vielen Dank. Könnten Sie erklären, warum es besser ist, WC()->checkout->get_value statt $checkout->get_value zu verwenden.

    – Evakos

    3. März 2018 um 15:57 Uhr

  • @Evakos Weil $checkout ist in diesem Hook nicht definiert. Beachte das auch $checkout und WC()->checkout sind dasselbe.

    – LoicTheAztec

    22. Mai 2018 um 2:51 Uhr

1228260cookie-checkBedingtes Validierungsproblem für benutzerdefinierte Checkout-Felder in Woocommerce

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy