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;
}
WooCommerce stellt standardmäßig auch ein Kontrollkästchen für die Verfügbarkeit bereit. Warum nicht stattdessen verwenden? –