Ich glaube, Sie woocommerce/templates/myaccount/form-login.php
Vorlage überschreiben haben, und damit haben Sie es geschafft, die billing_first_name
und billing_last_name
zu zeigen aber Sie vergessen, woocommerce_created_customer
Hook zu verwenden, der benötigt wird, um diese Daten in Ihrer Datenbank zu speichern.
Was werde ich vorschlagen, dass Sie die Vorlage halten, wie es ist, und jenes Feld durch function.php
Hier hinzuzufügen, ist der Code benutzerdefinierte Felder in WooCommerce Anmeldeformular hinzuzufügen:
/**
* To add WooCommerce registration form custom fields.
*/
function text_domain_woo_reg_form_fields() {
?>
<p class="form-row form-row-first">
<label for="billing_first_name"><?php _e('First name', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" />
</p>
<p class="form-row form-row-last">
<label for="billing_last_name"><?php _e('Last name', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
kommend Ihr zweiter Teil Ihrer Frage zur Validierung ist völlig optional und hängt von Ihrer Geschäftslogik ab, was Sie wollen, im Allgemeinen haben die meisten Seiten den Vornamen und den Nachnamen, aber es hängt wieder von Ihnen ab, wenn Sie es nicht tun Möchten Sie dies bestätigen, dann entfernen Sie <span class="required">*</span>
von oben Code und überspringen dies Sektion.
/**
* To validate WooCommerce registration form custom fields.
*/
function text_domain_woo_validate_reg_form_fields($username, $email, $validation_errors) {
if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) {
$validation_errors->add('billing_first_name_error', __('<strong>Error</strong>: First name is required!', 'text_domain'));
}
if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) {
$validation_errors->add('billing_last_name_error', __('<strong>Error</strong>: Last name is required!.', 'text_domain'));
}
return $validation_errors;
}
add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3);
Nun ist dies ein Hauptteil und das, was Sie wurde verpasst hatte, unter Code benötigt, um die benutzerdefinierten Daten zu speichern:
/**
* To save WooCommerce registration form custom fields.
*/
function text_domain_woo_save_reg_form_fields($customer_id) {
//First name field
if (isset($_POST['billing_first_name'])) {
update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
}
//Last name field
if (isset($_POST['billing_last_name'])) {
update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
}
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');
Alle oben genannten Code geht in function.php
Datei Ihrer aktiven untergeordnetes Thema (oder Thema). Oder auch in beliebigen Plugin-PHP-Dateien.
Der Code ist getestet und voll funktionsfähig.
Hoffe, das hilft!