2017-11-28 2 views
0

사용자 계정 관리 인터페이스에서 간단한 체크 박스 필드를 설정했습니다. 여기에 내가 저장/표시하고 방법입니다사용자 admin - WooCommerce에서 사용자 정의 메타로 체크 박스 설정

function show_free_ground_field($user) { 
?> 

    <h3>Free Ground Shipping</h3> 

    <table class="form-table"> 

     <tr> 
      <th>Free ground for order > $1000</th> 

      <td> 
       <?php 
       woocommerce_form_field('freeGround', array(
        'type'  => 'checkbox', 
        'class'  => array('input-checkbox'), 
        'label'  => __('Yes'), 
       ), ''); 


       ?> 

      </td> 
     </tr> 

    </table> 
<?php 
} 
add_action('show_user_profile', 'show_free_ground_field'); 
add_action('edit_user_profile', 'show_free_ground_field'); 

function save_free_ground_field($user_id) { 

    if (!current_user_can('edit_user', $user_id)){ 
     return false; 
    } 
    if (! empty($_POST['freeGround'])){ 
     update_usermeta($user_id, 'freeGround', $_POST['freeGround']); 
    } 
} 
add_action('personal_options_update', 'save_free_ground_field'); 
add_action('edit_user_profile_update', 'save_free_ground_field'); 

이 잘 표시,하지만 난 그것을 확인하고 체크 박스를 저장 한 후 동일한 사용자를 다시 방문을 선택하지. 어떻게 수정해야합니까?

답변

1

당신은 첫 번째 함수에서이 체크 박스 필드에 저장된 값을 얻을 필요합니다

add_action('show_user_profile', 'show_free_ground_field'); 
add_action('edit_user_profile', 'show_free_ground_field'); 
function show_free_ground_field($user) { 
    ?> 
    <h3>Free Ground Shipping</h3> 
    <table class="form-table"> 
     <tr> 
      <th>Free ground for order > $1000</th> 
      <td> 
    <?php 

    $freeGround = get_user_meta($user->id, 'freeGround', true); 
    if (empty($freeGround)) $freeGround = ''; 

    woocommerce_form_field('freeGround', array(
     'type'  => 'checkbox', 
     'class'  => array('input-checkbox'), 
     'label'  => __('Yes'), 
    ), $freeGround); 

    ?> 
      </td> 
     </tr> 
    </table> 
    <?php 
} 

이 마법처럼 지금

+0

작품을 작동합니다, 감사합니다! –

관련 문제