簡體   English   中英

在我的帳戶上添加手機字段 > 在 Woocommerce 中編輯帳戶

[英]Add a mobile phone field on My account > edit account in Woocommerce

我的問題:如何在Woocommerce的my-account/edit-account/頁面添加手機字段(相關模板: form-edit-account.php文件)

就像在以下答案線程中一樣:
在 WooCommerce 我的帳戶 > 帳戶詳細信息中保存自定義字段電話號碼的值

但是此答案代碼不完整,因為缺少一些掛鈎函數。 感謝任何幫助來完成一些事情,這意味着現場顯示。

您有 3 個選項可以在我的帳戶 > 編輯帳戶頁面上顯示自定義手機字段:

1) 作為使用woocommerce_edit_account_form_start動作鈎子的第一個字段(見下文)。

2) 在使用woocommerce_edit_account_form動作鈎子的現有字段之后

// Display the mobile phone field
// add_action( 'woocommerce_edit_account_form_start', 'add_billing_mobile_phone_to_edit_account_form' ); // At start
add_action( 'woocommerce_edit_account_form', 'add_billing_mobile_phone_to_edit_account_form' ); // After existing fields
function add_billing_mobile_phone_to_edit_account_form() {
    $user = wp_get_current_user();
    ?>
     <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="billing_mobile_phone"><?php _e( 'Mobile phone', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_mobile_phone" id="billing_mobile_phone" value="<?php echo esc_attr( $user->billing_mobile_phone ); ?>" />
    </p>
    <?php
}

// Check and validate the mobile phone
add_action( 'woocommerce_save_account_details_errors','billing_mobile_phone_field_validation', 20, 1 );
function billing_mobile_phone_field_validation( $args ){
    if ( isset($_POST['billing_mobile_phone']) && empty($_POST['billing_mobile_phone']) )
        $args->add( 'error', __( 'Please fill in your Mobile phone', 'woocommerce' ),'');
}

// Save the mobile phone value to user data
add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_mobile_phone', 20, 1 );
function my_account_saving_billing_mobile_phone( $user_id ) {
    if( isset($_POST['billing_mobile_phone']) && ! empty($_POST['billing_mobile_phone']) )
        update_user_meta( $user_id, 'billing_mobile_phone', sanitize_text_field($_POST['billing_mobile_phone']) );
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。

3)在特定位置,通過主題覆蓋myaccount/form-edit-account.php模板文件,如本文檔所述 這個答案線程上......

在這種情況下,您需要在模板中添加以下 html 代碼就像在這個答案線程中一樣

 <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="billing_mobile_phone"><?php _e( 'Mobile phone', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="billing_mobile_phone" id="billing_mobile_phone" value="<?php echo esc_attr( $user->billing_mobile_phone ); ?>" />
</p>

在最后一種情況下,您需要在主題的 function.php 文件中添加第 2 節(驗證和保存)中最后兩個掛鈎的函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM