2017-10-23 9 views
0


Woocommerce 사용자 계정 등록 양식에 일부 사용자 정의 필드를 추가하려고합니다.
이름과 성을 성공적으로 추가했습니다. T & C 계약 확인란을 추가하려고합니다. 선택하지 않으면 사용자가 등록을 계속할 수 없습니다.
유효성 확인 작업을 수행 할 수 없도록 올바르게 표시 할 수있었습니다. 내가 뭘 잘못 했니?
Woocommerce 계정 등록에서 확인란을 선택하지 않은 경우 오류가 발생합니다.

추신. 알다시피 페이지를 다시로드 할 필요없이 이러한 수표를 실시간으로 사용할 수 있습니까?
감사
내가 사용한 코드 :

add_action('woocommerce_register_form_start', 'cbi_custom_woo_account_registration'); 

function cbi_custom_woo_account_registration() { 
    ?> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first"> 
    <label for="reg_billing_first_name"><?php _e('First name', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" 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="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
    <label for="reg_billing_last_name"><?php _e('Last name', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" 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> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
    <label for="accept_terms"><?php _e('I agree TeC', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" value="<?php if (! empty($_POST['accept_terms'])) esc_attr_e($_POST['accept_terms']); ?>" /> 
    </p> 

    <div class="clear"></div> 

    <?php 
} 

// 2. VALIDATE FIELDS 

add_filter('woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3); 

function cbi_validate_custom_reg_fields($errors, $username, $email) { 
    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) { 
     $errors->add('billing_first_name_error', __('First name is required!', 'custom-cbi-parts')); 
    } 
    if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) { 
     $errors->add('billing_last_name_error', __('Last name is required!.', 'custom-cbi-parts')); 
    } 
    if (isset($_POST['accept_terms']) && $_POST['accept_terms'] == 0) { 
     $errors->add('accept_terms_error', __('You must accept Terms and Conditions!.', 'custom-cbi-parts')); 
    } 
    return $errors; 
} 
+0

WooCommerce는 기본적으로 T & C 확인란을 제공합니다. 왜 대신 사용하지 않습니까? –

답변

0

난 당신의 코드를 해결했습니다.

add_action('woocommerce_register_form_start', 'cbi_custom_woo_account_registration'); 

function cbi_custom_woo_account_registration() { 
    ?> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first"> 
    <label for="reg_billing_first_name"><?php _e('First name', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" 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="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
    <label for="reg_billing_last_name"><?php _e('Last name', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" 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> 

    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
    <label for="accept_terms"><?php _e('I agree TeC', 'custom-cbi-parts'); ?> <span class="required">*</span></label> 
    <input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" <?php if(isset($_POST['accept_terms'])){ echo "checked"; } ?> /> 
    </p> 

    <div class="clear"></div> 

    <?php 
} 

// 2. VALIDATE FIELDS 

add_action('woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3); 

function cbi_validate_custom_reg_fields($errors, $username, $email) { 

    if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) { 
     $errors->add('billing_first_name_error', __('First name is required!', 'custom-cbi-parts')); 
    } 
    if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) { 
     $errors->add('billing_last_name_error', __('Last name is required!.', 'custom-cbi-parts')); 
    } 
    if (!isset($_POST['accept_terms'])) { 
     $errors->add('accept_terms_error', __('You must accept Terms and Conditions!.', 'custom-cbi-parts')); 
    } 
    return $errors; 
} 
+0

안녕하세요, 답변 해 주셔서 감사합니다. 그러나 이것은 사용자 계정 페이지의 등록 양식입니다. 어쨌든 작동할까요? – Pietro

+0

오 !! 내 나쁜, 나는 내 대답을 편집 확인하시기 바랍니다. –

+0

1. add_action 대신 add_filter를 사용하고 있습니다. 2. 값 필드를 채워서 확인란을 채우지 않으면 체크 된 속성이 있습니다. 3. $ _POST [ 'access_terms]가 1 또는 0을 반환하지 않습니다. 선택하지 않으면 게시되지 않습니다. 선택하면 빈 값으로 게시됩니다. 그래서이 경우에는! isset ($ _ POST [ 'accept_terms'])를 체크하는 것이 낫다. –