1

logre añadir mediante código campos extras en el registro de woocommerce, funciona al registrarse en el frontend, así como en el backend del editor de usuarios.

BACKEND

// Agregamos los campos adicionales a Tu Perfil y Editar Usuario
function ammbio_nuevos_fields( $user ) {
  $ammbiophone = esc_attr( get_the_author_meta( 'event_ammbiophone', $user->ID ) );
  $ammbiofecha = esc_attr( get_the_author_meta( 'event_ammbiofecha', $user->ID ) );  
  ?>
<div class="hijos_colorea" style="background-color: bisque!important; padding: 50px!important;">
  <h3>Pruebas</h3>
   
  <table class="form-table">
    <tr>
      <th><label for="user_town">Telefono</label></th>
      <td><input type="text" name="event_ammbiophone" id="event_ammbiophone" class="regular-text" value="<?php echo $ammbiophone;?>" /></td>
    </tr>
 <tr>
      <th><label for="user_town">Fecha Nacimiento</label></th>
      <td><input type="text" name="event_ammbiofecha" id="event_ammbiofecha" class="regular-text" value="<?php echo $ammbiofecha;?>" />
   </td>
    </tr>
  </table>
  </div>
 
<?php }
 
add_action( 'show_user_profile', 'ammbio_nuevos_fields' );
add_action( 'edit_user_profile', 'ammbio_nuevos_fields' );
 
 
//Guardamos los datos personalizados
// Guardamos los campos adicionales en base de datos
function ammbio_guardar_nuevos_fields ($user_id) {
  if ( isset($_POST['event_ammbiophone']) ){
    update_user_meta($user_id, 'event_ammbiophone', sanitize_text_field($_POST['event_ammbiophone']));
  }
  
   if ( isset($_POST['event_ammbiofecha']) ){
    update_user_meta($user_id, 'event_ammbiofecha', sanitize_text_field($_POST['event_ammbiofecha']));
  }
} 
add_action( 'personal_options_update', 'ammbio_guardar_nuevos_fields' );
add_action( 'edit_user_profile_update', 'ammbio_guardar_nuevos_fields' );

FRONDEND REGISTRO EN WOOCOMMERCE

/* Campo extra en registro de WooCommerce */
add_action( 'woocommerce_register_form', 'ammbiophone_nuevos_fields_woo' );
function ammbiophone_nuevos_fields_woo(){
woocommerce_form_field(
'event_ammbiophone',
array(
'type' => 'text',
'required' => true, // esto añade un asterisco
'label' => 'Telefono'
),
( isset($_POST['event_ammbiophone']) ? $_POST['event_ammbiophone'] : '' )
);
}
//Añadimos validación del campo
add_action( 'woocommerce_register_post', 'ammbiophone_validar_campos', 10, 3 );
function ammbiophone_validar_campos( $username, $email, $errors ) {
if ( empty( $_POST['event_ammbiophone'] ) ) {
$errors->add( 'error_event_ammbiophone', '¡En serio, queremos saberlo!' );
}
}
//Añadimos el campo a la base de datos
add_action( 'woocommerce_created_customer', 'ammbiophone_guardar_campos_registro' );
function ammbiophone_guardar_campos_registro( $customer_id ){
if ( isset( $_POST['event_ammbiophone'] ) ) {
update_user_meta( $customer_id, 'event_ammbiophone', wc_clean( $_POST['event_ammbiophone'] ) );
}
}

/* Campo extra en registro fecha de WooCommerce */
add_action( 'woocommerce_register_form', 'ammbiofecha_nuevos_fields_woo' );
function ammbiofecha_nuevos_fields_woo(){
woocommerce_form_field(
'event_ammbiofecha',
array(
'type' => 'text',
'required' => true, // esto añade un asterisco
'label' => 'Fecha de Nacimiento'
),
( isset($_POST['event_ammbiofecha']) ? $_POST['event_ammbiofecha'] : '' )
);
}
//Añadimos validación del campo
add_action( 'woocommerce_register_post', 'ammbiofecha_validar_campos', 10, 3 );
function ammbiofecha_validar_campos( $username, $email, $errors ) {
if ( empty( $_POST['event_ammbiofecha'] ) ) {
$errors->add( 'error_event_ammbiofecha', '¡En serio, queremos saberlo!' );
}
}
//Añadimos el campo a la base de datos
add_action( 'woocommerce_created_customer', 'ammbiofecha_guardar_campos_registro' );
function ammbiofecha_guardar_campos_registro( $customer_id ){
if ( isset( $_POST['event_ammbiofecha'] ) ) {
update_user_meta( $customer_id, 'event_ammbiofecha', wc_clean( $_POST['event_ammbiofecha'] ) );
}
}

AHORA...

Trato de hacer las modificaciones en FRONTEND del formulario de edit-account de woocommerce, para que mis clientes puedan ver y modificar desde ahi los campos previamente mencionados. Logré que el campo personalizado se muestre al igual que el valor introducido por el cliente, sin embargo, no logro concretar la acción para que se modifique, aquí el código que utilizo:

<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="event_ammbiophone"><?php esc_html_e( 'Telefono', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="event_ammbiophone" id="event_ammbiophone" value="<?php echo esc_attr( $user->event_ammbiophone ); ?>" />
</p>
<div class="clear"></div>

Veo en la parte del botón submit llama a una funcion, aqui se las muestro:

<p>
        <?php wp_nonce_field( 'save_account_details', 'save-account-details-nonce' ); ?>
        <button type="submit" class="woocommerce-Button button" name="save_account_details" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>"><?php esc_html_e( 'Save changes', 'woocommerce' ); ?></button>
        <input type="hidden" name="action" value="save_account_details" />
    </p>

¿Esta es la función que necesito modificar para lograr que los cambios realizados por el cliente se apliquen?

¿Cómo lo puedo hacer sin que afecte los campos predeterminados de woocommerce?

Porfavor y Gracias

ACTUALIZACIÓN Encontre la funcion del save_changes intento agregar la funcion pero sigo sin lograrlo, se los comparto:

/**
     * Save the password/account details and redirect back to the my account page.
     */
    public static function save_account_details() {
        $nonce_value = wc_get_var( $_REQUEST['save-account-details-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.

        if ( ! wp_verify_nonce( $nonce_value, 'save_account_details' ) ) {
            return;
        }

        if ( empty( $_POST['action'] ) || 'save_account_details' !== $_POST['action'] ) {
            return;
        }

        wc_nocache_headers();

        $user_id = get_current_user_id();

        if ( $user_id <= 0 ) {
            return;
        }

        $account_first_name   = ! empty( $_POST['account_first_name'] ) ? wc_clean( wp_unslash( $_POST['account_first_name'] ) ) : '';
        $account_last_name    = ! empty( $_POST['account_last_name'] ) ? wc_clean( wp_unslash( $_POST['account_last_name'] ) ) : '';
        $account_display_name = ! empty( $_POST['account_display_name'] ) ? wc_clean( wp_unslash( $_POST['account_display_name'] ) ) : '';
        $account_email        = ! empty( $_POST['account_email'] ) ? wc_clean( wp_unslash( $_POST['account_email'] ) ) : '';
        $pass_cur             = ! empty( $_POST['password_current'] ) ? $_POST['password_current'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
        $pass1                = ! empty( $_POST['password_1'] ) ? $_POST['password_1'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
        $pass2                = ! empty( $_POST['password_2'] ) ? $_POST['password_2'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
        $ammbiophone          = ! empty( $_POST['event_ammbiophone'] ) ? wc_clean( wp_unslash( $_POST['event_ammbiophone'] ) ) : '';
        $save_pass            = true;

        // Current user data.
        $current_user       = get_user_by( 'id', $user_id );
        $current_first_name = $current_user->first_name;
        $current_last_name  = $current_user->last_name;
        $current_email      = $current_user->user_email;
        $ammbiophone        = $current_user->event_ammbiophone;

        // New user data.
        $user               = new stdClass();
        $user->ID           = $user_id;
        $user->first_name   = $account_first_name;
        $user->last_name    = $account_last_name;
        $user->display_name = $account_display_name;
        $user->event_ammbiophone = $ammbiophone;

        // Prevent display name to be changed to email.
        if ( is_email( $account_display_name ) ) {
            wc_add_notice( __( 'Display name cannot be changed to email address due to privacy concern.', 'woocommerce' ), 'error' );
        }

        // Handle required fields.
        $required_fields = apply_filters(
            'woocommerce_save_account_details_required_fields',
            array(
                'account_first_name'   => __( 'First name', 'woocommerce' ),
                'account_last_name'    => __( 'Last name', 'woocommerce' ),
                'account_display_name' => __( 'Display name', 'woocommerce' ),
                'account_email'        => __( 'Email address', 'woocommerce' ),
                'event_ammbiophone'    => __( 'Telefono', 'woocommerce' ),
            )
        );

        foreach ( $required_fields as $field_key => $field_name ) {
            if ( empty( $_POST[ $field_key ] ) ) {
                /* translators: %s: Field name. */
                wc_add_notice( sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . esc_html( $field_name ) . '</strong>' ), 'error', array( 'id' => $field_key ) );
            }
        }

        if ( $account_email ) {
            $account_email = sanitize_email( $account_email );
            if ( ! is_email( $account_email ) ) {
                wc_add_notice( __( 'Please provide a valid email address.', 'woocommerce' ), 'error' );
            } elseif ( email_exists( $account_email ) && $account_email !== $current_user->user_email ) {
                wc_add_notice( __( 'This email address is already registered.', 'woocommerce' ), 'error' );
            }
            $user->user_email = $account_email;
        }

        if ( ! empty( $pass_cur ) && empty( $pass1 ) && empty( $pass2 ) ) {
            wc_add_notice( __( 'Please fill out all password fields.', 'woocommerce' ), 'error' );
            $save_pass = false;
        } elseif ( ! empty( $pass1 ) && empty( $pass_cur ) ) {
            wc_add_notice( __( 'Please enter your current password.', 'woocommerce' ), 'error' );
            $save_pass = false;
        } elseif ( ! empty( $pass1 ) && empty( $pass2 ) ) {
            wc_add_notice( __( 'Please re-enter your password.', 'woocommerce' ), 'error' );
            $save_pass = false;
        } elseif ( ( ! empty( $pass1 ) || ! empty( $pass2 ) ) && $pass1 !== $pass2 ) {
            wc_add_notice( __( 'New passwords do not match.', 'woocommerce' ), 'error' );
            $save_pass = false;
        } elseif ( ! empty( $pass1 ) && ! wp_check_password( $pass_cur, $current_user->user_pass, $current_user->ID ) ) {
            wc_add_notice( __( 'Your current password is incorrect.', 'woocommerce' ), 'error' );
            $save_pass = false;
        }

        if ( $pass1 && $save_pass ) {
            $user->user_pass = $pass1;
        }

1 respuesta 1

0

Lo logré! añadí lo siguiente:

if ( isset( $_POST['event_ammbiophone'] ) ) {
update_user_meta( $customer_id, 'event_ammbiophone', wc_clean( $_POST['event_ammbiophone'] ) );
}

Abajo de:

// Prevent display name to be changed to email.
        if ( is_email( $account_display_name ) ) {
            wc_add_notice( __( 'Display name cannot be changed to email address due to privacy concern.', 'woocommerce' ), 'error' );
        }

Nadie respondió a mi pregunta, pero pues aquí dejo la solución, a alguna persona le servirá en un futuro.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.