2017-10-23 1 views
0


Ich versuche, einige benutzerdefinierte Felder in der Woocommerce Benutzerkonto Registrierung Formular hinzuzufügen.
Ich habe erfolgreich hinzugefügt Vorname und Nachname, Nut ich versuche, ein T & C Vereinbarung Kontrollkästchen hinzufügen, dass, wenn es nicht ausgewählt ist es den Benutzer nicht die Registrierung fortsetzen.
Ich habe es richtig angezeigt, ich kann die Validierung nicht funktionieren. Was mache ich falsch?
Geben Sie einen Fehler, wenn Checkbox nicht auf Woocommerce Account-Registrierung ausgewählt ist

PS. nur zu wissen, ist es möglich, diese Kontrollen live zu machen, ohne die Seite neu laden zu müssen?
Dank
Der Code, den ich verwendet habe:

add_action('woocommerce_register_form_start', 'cbi_custom_woo_account_registration'); 

function cbi_custom_woo_account_registration() { 
    ?> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first"> 
    <label for="reg_billing_first_name"><?php _e('First name', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_first_name" id="reg_billing_first_name" value="<?php if (! empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" /> 
    </p> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
    <label for="reg_billing_last_name"><?php _e('Last name', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_last_name" id="reg_billing_last_name" value="<?php if (! empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" /> 
    </p> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
    <label for="accept_terms"><?php _e('I agree TeC', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" value="<?php if (! empty($_POST['accept_terms'])) esc_attr_e($_POST['accept_terms']); ?>" /> 
    </p> 

    <div class="clear"></div> 

    <?php 
} 

// 2. VALIDATE FIELDS 

add_filter('woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3); 

function cbi_validate_custom_reg_fields($errors, $username, $email) { 
    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) { 
     $errors->add('billing_first_name_error', __('First name is required!', 'custom-cbi-parts')); 
    } 
    if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) { 
     $errors->add('billing_last_name_error', __('Last name is required!.', 'custom-cbi-parts')); 
    } 
    if (isset($_POST['accept_terms']) && $_POST['accept_terms'] == 0) { 
     $errors->add('accept_terms_error', __('You must accept Terms and Conditions!.', 'custom-cbi-parts')); 
    } 
    return $errors; 
} 
+0

WooCommerce stellt standardmäßig auch ein Kontrollkästchen für die Verfügbarkeit bereit. Warum nicht stattdessen verwenden? –

Antwort

0

Ich habe Ihren Code festgelegt.

add_action('woocommerce_register_form_start', 'cbi_custom_woo_account_registration'); 

function cbi_custom_woo_account_registration() { 
    ?> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first"> 
    <label for="reg_billing_first_name"><?php _e('First name', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_first_name" id="reg_billing_first_name" value="<?php if (! empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" /> 
    </p> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
    <label for="reg_billing_last_name"><?php _e('Last name', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_last_name" id="reg_billing_last_name" value="<?php if (! empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" /> 
    </p> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
    <label for="accept_terms"><?php _e('I agree TeC', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" <?php if(isset($_POST['accept_terms'])){ echo "checked"; } ?> /> 
    </p> 

    <div class="clear"></div> 

    <?php 
} 

// 2. VALIDATE FIELDS 

add_action('woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3); 

function cbi_validate_custom_reg_fields($errors, $username, $email) { 

    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) { 
     $errors->add('billing_first_name_error', __('First name is required!', 'custom-cbi-parts')); 
    } 
    if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) { 
     $errors->add('billing_last_name_error', __('Last name is required!.', 'custom-cbi-parts')); 
    } 
    if (!isset($_POST['accept_terms'])) { 
     $errors->add('accept_terms_error', __('You must accept Terms and Conditions!.', 'custom-cbi-parts')); 
    } 
    return $errors; 
} 
+0

Hallo, danke für deine Antwort. Dies ist jedoch das Registrierungsformular auf der Benutzerkontoseite. Wird es trotzdem funktionieren? – Pietro

+0

Oh !! meine schlechte, ich habe meine antwort bearbeitet bitte überprüfen. –

+0

1. Sie haben add_filter anstelle von add_action verwendet. 2. Checkbox wird nicht gefüllt, indem das Wertfeld gefüllt wird. Es gibt eine Checked-Eigenschaft. 3. $ _POST ['access_terms] gibt 1 oder 0 nicht zurück. Es wird nicht gesendet, wenn es nicht markiert ist. und mit leerem Wert gepostet, wenn aktiviert. Daher ist es besser, in diesem Fall! isset ($ _ POST ['accept_terms']) zu überprüfen –

Verwandte Themen