2017-11-28 8 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

作品を動作するはずです、ありがとうございました! –

関連する問題