WooCommerce 등록 양식에 사용자 정의 필드를 추가하는 방법
저도 비슷한 질문을 보았지만 해결책을 찾지 못했습니다.
WooCommerce 등록 양식에 사용자 지정 필드, 특히 이름 필드를 추가하려고 합니다.이 필드를 만들었지만 사용자가 로그인할 때 입력한 정보가 계정 세부 정보 페이지로 전달되지 않습니다.다른 튜토리얼에서는 필드의 유효성을 확인하는 것에 대해 언급했지만 저는 그것이 저와 관련이 있는지 아닌지 확신할 수 없습니다.저는 워드프레스 어린이 테마를 작업하고 있습니다.
코드를 보려면 codepad.org로 방문해 주시기 바랍니다.코드 샘플 옵션을 이용해서 여기에 코드를 붙여 넣으려고 했는데 제대로 작동하지 않습니다.
제가 제 자신을 분명하게 설명했길 바랍니다.만약 그렇지 않다면 제게 알려주시면 명확히 해두겠습니다.
덮어쓰기를 한 것 같습니다.woocommerce/templates/myaccount/form-login.php템플릿, 그리고 그렇게 함으로써 당신은 그 방법을 보여질 수 있었습니다.billing_first_name그리고.billing_last_name데이터를 데이터베이스에 저장하는 데 필요한 후크를 사용하는 것을 잊어버립니다.
제가 제안하는 것은 템플릿을 그대로 유지하고 해당 필드를 통해 추가하는 것을 추가하는 것입니다.function.php
WooCommerce 등록 양식에 사용자 정의 필드를 추가하는 코드는 다음과 같습니다.
/**
* 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');
검증에 관한 질문의 두 번째 부분에 관해서는 완전히 선택 사항이며, 일반적으로 대부분의 사이트에 이름과 성이 필요하다는 비즈니스 논리에 따라 결정됩니다. 하지만 이를 검증하지 않으려면 제거하십시오.<span class="required">*</span>위의 코드에서 이 섹션을 생략합니다.
/**
* 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);
이것이 주요 부분이며, 사용자 지정 데이터를 저장하기 위해 아래 코드가 필요합니다.
/**
* 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');
위의 모든 코드가 들어갑니다.function.php활성 하위 테마(또는 테마)의 파일입니다.또는 플러그인 php 파일에서도 마찬가지입니다.
코드가 테스트되고 완전히 작동합니다.
도움이 되길 바랍니다!
<?php
/**
* Add new register fields for WooCommerce registration
* To add WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_register_form', 'misha_add_register_form_field' );
function misha_add_register_form_field(){
woocommerce_form_field(
'billing_first_name',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'First name'
),
( isset($_POST['billing_first_name']) ? $_POST['billing_first_name'] : '' )
);
woocommerce_form_field(
'billing_last_name',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'Last name'
),
( isset($_POST['billing_last_name']) ? $_POST['billing_last_name'] : '' )
);
woocommerce_form_field(
'billing_phone',
array(
'type' => 'tel',
'required' => true, // just adds an "*"
'label' => 'Phone'
),
( isset($_POST['billing_phone']) ? $_POST['billing_phone'] : '' )
);
}
/**
* To validate WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_register_post', 'misha_validate_fields', 10, 3 );
function misha_validate_fields( $username, $email, $errors ) {
if ( empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', 'First name is required!' );
}
if ( empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', 'Last name is required!' );
}
if ( empty( $_POST['billing_phone'] ) ) {
$errors->add( 'billing_phone_error', 'Phone is required!' );
}
}
/**
* To save WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
function misha_save_register_fields( $customer_id ){
//First name field
if ( isset( $_POST['billing_first_name'] ) ) {
//update_user_meta( $customer_id, 'country_to_visit', wc_clean( $_POST['country_to_visit'] ) );
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']));
}
// WooCommerce billing phone
if ( isset( $_POST['billing_phone'] ) ) {
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
}
?>
필터 후크를 사용하여 Woocommerce 등록 양식 사용자 정의 필드 추가woocommerce_forms_fieldnonce verification 을 사용하여 데이터를 저장합니다.
/**
* Adding more fields to Woocommerce Registration form and My account page
*/
function how_woocommerce_registration_form_fields() {
return apply_filters( 'woocommerce_forms_field', array(
'billing_company' => array(
'type' => 'text',
'label' => __( 'Company Name', ' how' ),
'required' => false,
),
'how_user_job_title' => array(
'type' => 'text',
'label' => __( 'Job Title', ' how' ),
'required' => false,
),
'how_user_industry' => array(
'type' => 'select',
'label' => __( 'Industry', 'how' ),
'options' => array(
'' => __( 'Select an Industry', 'how' ),
'Lending' => __( 'Lending', 'how' ),
'Real Estate' => __( 'Real Estate', 'how' ),
'Investment' => __( 'Investment', 'how' ),
'Servicing' => __( 'Servicing', 'how' ),
'Other' => __( 'Other', 'how' ),
'Mortgage Servicing' => __( 'Mortgage Servicing', 'how' ),
'Mortgage Lending' => __( 'Mortgage Lending', 'how' ),
)
)
) );
}
function how_woocommerce_edit_registration_form() {
$fields = how_woocommerce_registration_form_fields();
foreach ( $fields as $key => $field_args ) {
woocommerce_form_field( $key, $field_args );
}
}
add_action( 'woocommerce_register_form', 'how_woocommerce_edit_registration_form', 15 );
/**
* Save registration form custom fields
*/
function wooc_save_extra_register_fields( $customer_id ) {
if( wp_verify_nonce( sanitize_text_field( $_REQUEST['woocommerce-register-nonce'] ), 'woocommerce-register' ) ) {
if ( isset( $_POST['billing_company'] ) ) {
update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
update_user_meta( $customer_id, 'shipping_company', sanitize_text_field( $_POST['billing_company'] ) );
}
if ( isset( $_POST['how_user_job_title'] ) ) {
update_user_meta( $customer_id, 'how_user_job_title', sanitize_text_field( $_POST['how_user_job_title'] ) );
}
if ( isset( $_POST['how_user_industry'] ) ) {
update_user_meta( $customer_id, 'how_user_industry', sanitize_text_field( $_POST['how_user_industry'] ) );
}
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
언급URL : https://stackoverflow.com/questions/40580190/how-to-add-custom-fields-to-woocommerce-registration-form
'codememo' 카테고리의 다른 글
| SQLPlus에서 SPOL을 사용할 때 데이터 행 사이에 빈 줄이 표시됩니다. (0) | 2023.10.20 |
|---|---|
| 페이지 로드 완료 후 함수 실행 (0) | 2023.10.20 |
| 공식 파라미터란 무엇입니까? (0) | 2023.10.20 |
| 페이지 새로 고침 없이 ajax를 사용하여 acf_form을 저장하는 방법 (0) | 2023.10.20 |
| XSL: xsl: template의 match="/"의 의미 (0) | 2023.10.20 |