Установка флажков в качестве пользовательских мета пользователей в пользовательском админке — 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' );

Он отображается нормально, но если я отмечаю его и повторно посещаю того же пользователя после сохранения, флажок снят. Как мне это исправить?

0

Решение

Вам необходимо получить сохраненное значение для этого поля флажка в первой функции:

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
}

Это должно работать сейчас

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector