@Genri_Rus

При добавлении метаполей ошибка: warning: in_array() expects parameter 2 to be array?

Добавляю 3 чекбокса в админку:

/ New Multi Checkbox field for woocommerce backend
function woocommerce_wp_multi_checkbox( $field ) {
    global $thepostid, $post;

    $field['value'] = get_post_meta( $thepostid, $field['id'], true );

    $thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
    $field['class']         = isset( $field['class'] ) ? $field['class'] : 'select short';
    $field['style']         = isset( $field['style'] ) ? $field['style'] : '';
    $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
    $field['value']         = isset( $field['value'] ) ? $field['value'] : array();
    $field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
    $field['desc_tip']      = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;

    echo '<fieldset class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">
    <legend>' . wp_kses_post( $field['label'] ) . '</legend>';

    if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
        echo wc_help_tip( $field['description'] );
    }

    echo '<ul class="wc-radios">';

    foreach ( $field['options'] as $key => $value ) {

        echo '<li><label><input
                name="' . esc_attr( $field['name'] ) . '"
                value="' . esc_attr( $key ) . '"
                type="checkbox"
                class="' . esc_attr( $field['class'] ) . '"
                style="' . esc_attr( $field['style'] ) . '"
                ' . ( in_array( $key, $field['value'] ) ? 'checked="checked"' : '' ) . ' /> ' . esc_html( $value ) . '</label>
        </li>';
    }
    echo '</ul>';

    if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
        echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
    }

    echo '</fieldset>';
}

// Add custom multi-checkbox field for product general option settings
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_settings_fields', 20 );
function add_custom_settings_fields() {
    global $post;

    echo '<div class="options_group hide_if_variable"">'; // Hidding in variable products

    woocommerce_wp_multi_checkbox( array(
        'id'    => '_custom_level',
        'name'  => '_custom_level[]',
        'label' => __('Levels', 'woocommerce'),
        'options' => array(
            'MBO'   => __( 'MBO', 'woocommerce' ),
            'HBO'   => __( 'HBO', 'woocommerce' ),
            'WO'    => __( 'WO', 'woocommerce' )
        )
    ) );

    echo '</div>';
}

// Save custom multi-checkbox fields to database when submitted in Backend (for all other product types)
add_action( 'woocommerce_process_product_meta', 'save_product_options_custom_fields', 30, 1 );
function save_product_options_custom_fields( $post_id ){
    if( isset( $_POST['_custom_level'] ) ){
        $post_data = $_POST['_custom_level'];
        // Data sanitization
        $sanitize_data = array();
        if( is_array($post_data) && sizeof($post_data) > 0 ){
            foreach( $post_data as $value ){
                $sanitize_data[] = esc_attr( $value );
            }
        }
        update_post_meta( $post_id, '_custom_level', $sanitize_data );
    }
}


В админку все добавляется, но интерпретатор ругается на эту строчку: '
. ( in_array( $key, $field['value'] ) ? 'checked="checked"' : '' ) . ' /> '
  • Вопрос задан
  • 391 просмотр
Решения вопроса 1
@Genri_Rus Автор вопроса
Спасибо огромное Валерий Чмых за устранение ошибки:in_array() expects parameter 2 to be array
Кому интересно полноценное добавление в админку чекбоксов (аналог радиокнопок) в woocommerce, то вот решение:
/ New Multi Checkbox field for woocommerce backend
function woocommerce_wp_multi_checkbox( $field ) {
    global $thepostid, $post;

    $field['value'] = get_post_meta( $thepostid, $field['id'], true );

    $thepostid              = empty( $thepostid ) ? $post->ID : $thepostid;
    $field['class']         = isset( $field['class'] ) ? $field['class'] : 'select short';
    $field['style']         = isset( $field['style'] ) ? $field['style'] : '';
    $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
    $field['value']         = isset( $field['value'] ) ? $field['value'] : array();
    $field['name']          = isset( $field['name'] ) ? $field['name'] : $field['id'];
    $field['desc_tip']      = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;

    echo '<fieldset class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">
    <legend>' . wp_kses_post( $field['label'] ) . '</legend>';

    if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
        echo wc_help_tip( $field['description'] );
    }

    echo '<ul class="wc-radios">';

    foreach ( $field['options'] as $key => $value ) {
        echo '<li><label><input
                name="' . esc_attr( $field['name'] ) . '"
                value="' . esc_attr( $key ) . '"
                type="checkbox"
                class="' . esc_attr( $field['class'] ) . '"
                style="' . esc_attr( $field['style'] ) . '"
                ' . (array_search($key, is_array($field["value"]) ? $field["value"]:array($field["value"])) !== false ? 'checked="checked"': '' ). ' /> ' . esc_html( $value ) . '</label>
        </li>';
    }
    echo '</ul>';

    if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) {
        echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
    }

    echo '</fieldset>';
}

// Add custom multi-checkbox field for product general option settings
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_settings_fields', 20 );
function add_custom_settings_fields() {
    global $post;

    echo '<div class="options_group hide_if_variable"">'; // Hidding in variable products

    woocommerce_wp_multi_checkbox( array(
        'id'    => '_custom_level',
        'name'  => '_custom_level[]',
        'label' => __('Levels', 'woocommerce'),
        'options' => array(
            'MBO'   => __( 'MBO', 'woocommerce' ),
            'HBO'   => __( 'HBO', 'woocommerce' ),
            'WO'    => __( 'WO', 'woocommerce' )
        )
    ) );

    echo '</div>';
}

// Save custom multi-checkbox fields to database when submitted in Backend (for all other product types)
add_action( 'woocommerce_process_product_meta', 'save_product_options_custom_fields', 30, 1 );
function save_product_options_custom_fields( $post_id ){
    if( isset( $_POST['_custom_level'] ) ){
        $post_data = $_POST['_custom_level'];
        // Data sanitization
        $sanitize_data = array();
        if( is_array($post_data) && sizeof($post_data) > 0 ){
            foreach( $post_data as $value ){
                $sanitize_data[] = esc_attr( $value );
            }
        }
        update_post_meta( $post_id, '_custom_level', $sanitize_data );
    } else {
    delete_post_meta( $post_id, '_custom_level' );
  }
}
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
@Arik
$field['value'] = get_post_meta( $thepostid, $field['id'], true );

тут нужен массив, а у вас не ясно что
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы