本文介绍了Woocommerce后端中的多个复选框字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在woocommerce后端中添加一个自定义字段,用户可以在其中选择多个复选框以达到特定级别.

是否可以创建多个复选框?到目前为止,我有这个:

  woocommerce_wp_checkbox(大批('id'=>'_custom_product_niveau_field','type'=>复选框",'标签'=>__('Niveau','woocommerce'),'options'=>大批('MBO'=>__('MBO','woocommerce'),'HBO'=>__('HBO','woocommerce'),'WO'=>__('WO','woocommerce'))) 

但这不起作用... woocommerce_wp_checkbox是否对此提供支持?

解决方案

2021更新

可以通过以下方式创建自定义函数:

 //woocommerce后端的新的多复选框"字段函数woocommerce_wp_multi_checkbox($ field){全球$ thepostid,$ post;if(!$ thepostid){$ thepostid = $ post-> ID;}$ field ['value'] = get_post_meta($ thepostid,$ field ['id'],true);$ thepostid = empty($ thepostid)吗?$ post-> ID:$ thepostid;$ field ['class'] = isset($ field ['class'])吗?$ field ['class']:'选择短号';$ 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']).'_场地 ' .esc_attr($ field ['wrapper_class']).'"<传奇>'.wp_kses_post($ field ['label']).'</legend>';if(!empty($ field ['description'])&& false!== $ field ['desc_tip']){回声wc_help_tip($ field ['description']);}echo'< ul class ='wc-radios'>';foreach($ field ['options'] as $ key => $ value){echo'< li><标签><输入名称=".esc_attr($ field ['name'])."值=".esc_attr($ key)."type ="checkbox"class =".esc_attr($ field ['class'])."样式=".esc_attr($ field ['style'])."'.(is_array($ field ['value'])&& in_array($ key,$ field ['value'])?'checked ='checked'':').'/>'.esc_html($ value).'</label></li>';}回声'</ul>';if(!empty($ field ['description'])&& false === $ field ['desc_tip']){echo'< span class ='description'>'.wp_kses_post($ field ['description']).'</span>';}回声'</fieldset>';} 

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Ive been trying to add a custom field in woocommerce backend where users can multiselect checkboxes for a certain level.

Is it possible to create multiple checkboxes? So far i have this:

woocommerce_wp_checkbox(
    array(
        'id' => '_custom_product_niveau_field',
        'type' => 'checkbox',
        'label' => __('Niveau', 'woocommerce'),
        'options' => array(
            'MBO'   => __( 'MBO', 'woocommerce' ),
            'HBO'   => __( 'HBO', 'woocommerce' ),
            'WO' => __( 'WO', 'woocommerce' )
        )
    )

But that doesnt work... Does woocommerce_wp_checkbox have support for this?

解决方案

2021 Update

That is possible creating a custom function this way:

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

    if( ! $thepostid ) {
        $thepostid = $post->ID;
    }

    $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'] ) . '"
                ' . ( is_array( $field['value'] ) && 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>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


Usage example (for a simple product):

// 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 );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于Woocommerce后端中的多个复选框字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 21:17