Woocommerce註冊頁面沒有電話欄位, 有甚麼解決方案?

Woocommerce註冊頁面沒有電話欄位, 有甚麼解決方案?

如果你自己架設獨立站或是網路商店, 使用Wordpress + Woocommercee. 一定會發現Woocommerc註冊頁面沒有電話欄位, 萬一要跟顧客聯絡要透過email, 卻不知道客戶何時會回覆, 有些困擾。


看了網路上的教學, 有個免費的方法, 也不要寫程式碼, 只要複製貼上即可到左側功能表找到外觀下階的佈景主題編輯器檔案點下去,


在右側的"佈景主題檔案"中, 找到WooCommerce下的function.php檔案。這裡要注意, 不要用上面佈景主題的function.php檔案。


點一下function.php, 左邊框框內出現程式碼, 將游標移到最下, 然後將下面程式碼複製貼到程式的下面, 再按"更新檔案"。


要複製貼上的程式碼

function wooc_extra_register_fields() {?> <p class="form-row form-row-wide"> <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?></label> <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" /> </p> <p class="form-row form-row-first"> <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label> <input type="text" class="input-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'] ); ?>" /> </p> <p class="form-row form-row-last"> <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label> <input type="text" class="input-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'] ); ?>" /> </p> <div class="clear"></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' ) ); } 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' );


這個方法是參考以下的網路文章, 謝謝你

https://willcoast.com/%E5%A6%82%E4%BD%95%E5%9C%A8woocommerce%E8%A8%BB%E5%86%8A%E8%A1%A8%E5%96%AE%E6%B7%BB%E5%8A%A0%E5%A7%93%E5%90%8D%E7%AD%89%E5%AD%97%E6%AE%B5/

沒有留言 :