【问题标题】:How to change the added phone value in edit-account template form?如何更改编辑帐户模板表单中添加的电话值?
【发布时间】:2021-12-24 20:11:16
【问题描述】:

我需要在注册 woocommerce 表单中添加字段“名字”、“姓氏”和“电话”。 我找到了那个决定。

function wooc_extra_register_fields() {?>
         <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">           
           <input type="text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" placeholder="<?php _e( 'First Name', 'mydomain' ) ?>" />
        </div>
        <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">       
            <input type="text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" placeholder="<?php _e( 'Last Name', 'mydomain' ) ?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
        </div>

         <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">       
            <input type="text" name="billing_phone" id="reg_billing_phone" placeholder="<?php _e( 'Phone', 'woocommerce' ); ?>" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
        </div>
 
       <?php
 }
 add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

 /**

* register fields Validating.

*/

function wooc_validate_extra_register_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!', 'woocommerce' ) );

      }

      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!.', 'woocommerce' ) );

      }


      if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {

             $validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!.', 'woocommerce' ) );

      }

         return $validation_errors;
}

add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST['billing_phone'] ) ) {
                 // Phone input filed which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
          }
      if ( isset( $_POST['billing_first_name'] ) ) {
             //First name field which is by default
             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
             // First name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
             // Last name field which is by default
             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
             // Last name field which is used in WooCommerce
             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );      }

}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' ); 

在 myaccount form-edit-account.php 模板中是这样的

<form class="woocommerce-EditAccountForm edit-account" action="" method="post" <?php do_action( 'woocommerce_edit_account_form_tag' ); ?> >

   <?php do_action( 'woocommerce_edit_account_form_start' ); ?>

   <p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first page-inside-p">
       <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="account_first_name" id="account_first_name" autocomplete="given-name" value="<?php echo esc_attr( $user->first_name ); ?>" placeholder="<?php esc_html_e( 'First name', 'woocommerce' ); ?>" />
   </p>
   <p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last page-inside-p">        
       <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="account_last_name" id="account_last_name" autocomplete="family-name" value="<?php echo esc_attr( $user->last_name ); ?>" placeholder="<?php esc_html_e( 'Last name', 'woocommerce' ); ?>" />
   </p>
   <div class="clear"></div>

   <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide page-inside-p">        
       <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="account_display_name" id="account_display_name" value="<?php echo esc_attr( $user->display_name ); ?>" placeholder="<?php esc_html_e( 'Display name', 'woocommerce' ); ?>" /> 
   </p>
   <div class="clear"></div>

   <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide mb-20 page-inside-p">      
       <input type="email" class="woocommerce-Input woocommerce-Input--email input-text form-control" name="account_email" id="account_email" autocomplete="email" value="<?php echo esc_attr( $user->user_email ); ?>" placeholder="<?php esc_html_e( 'Email address', 'woocommerce' ); ?>" />
   </p>

        <div class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide form-group">       
           <input type="text" name="billing_phone" id="reg_billing_phone" placeholder="<?php _e( 'Phone', 'woocommerce' ); ?>" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" class="woocommerce-Input woocommerce-Input--text input-text form-control" size="25" />
       </div>....etc

名字和姓氏可以正确更改,但我在该页面中看不到电话号码的值,无法更新以保存。正确的方法是什么?我复制了与注册表相同的电话字段,但似乎这是错误的方式。如何正确显示和更改?

【问题讨论】:

    标签: wordpress woocommerce woocommerce-theming


    【解决方案1】:

    代码的第一部分用于更新用户字段。您的 HTML 输入标签中有“名称”属性。提交这些值是通过这些名称实现的。

    提交后,如果验证了这些字段,则使用update_user_meta 函数使用给定字段的新值更新此用户的数据库记录。因此,$_POST['billing_phone'] 用于此目的,您无法使用它检索数据。

    问题出在您的“myaccount form-edit-account.php”中。如您所见,所有其他正确的部分都从用户对象中获取值。如value="&lt;?php echo esc_attr( $user-&gt;first_name ); ?&gt;" 获取当前用户的名字。

    对于电话号码,您应该使用 billing_phone 字段,因为您对该字段使用update_user_meta 方法,并且您可以使用get_user_meta 方法检索此更新数据。

    因此,将value="&lt;?php esc_attr_e( $_POST['billing_phone'] ); ?&gt;" 替换为正确的分配value="&lt;?php echo esc_attr( get_user_meta($user-&gt;id,'billing_phone',true) ); ?&gt;" 将解决您的问题。

    此外,要从我的帐户页面更新此电话号码 (billing_phone) 字段,您可以查看直接显示解决方案的答案:Add a custom field in Woocommerce Edit Account page

    希望这会有所帮助。

    【讨论】:

    • 我更换了,遗憾的是这没有帮助
    • 我在注册表中写的电话似乎很好,但它不想更新。或者相反 - 如果不显示,它会更新。不知道哪里有冲突
    • 也许我不明白主要问题。你能指定吗?是关于“注册 woocommerce 表单”还是“myaccount form-edit-account.php”?我写的解决方案是在我的帐户用户详细信息页面中显示计费电话。
    • 这是关于 myaccount form-edit-account.php 的。注册表中的电话显示正确,但我无法在我的帐户页面中更改其值。它在这里i83862.hostua5.fornex.host(the 域是技术性的)。注册表在弹出窗口中,帐户页面在这里i83862.hostua5.fornex.host/my-account/edit-account
    • 好的,我现在知道了。但我相信使用您的第一个代码,您也无法在我的帐户页面中显示电话号码,当您替换值时应该正确显示。要在我的帐户页面中更新此字段,请在此处尝试接受的答案:stackoverflow.com/questions/47599050/…
    猜你喜欢
    • 2018-02-05
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多