【问题标题】:Multi checkbox fields in Woocommerce backendWoocommerce 后端中的多复选框字段
【发布时间】:2018-11-20 20:00:36
【问题描述】:

我一直在尝试在 woocommerce 后端添加一个自定义字段,用户可以在其中多选某个级别的复选框。

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

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' )
        )
    )

但这不起作用... woocommerce_wp_checkbox 是否支持此功能?

【问题讨论】:

    标签: php wordpress checkbox woocommerce custom-fields


    【解决方案1】:

    2021 年更新

    2021 年更新 - 解决了以下问题:
    in_array() 其中第二个参数是开始时的字符串*。
    $thepostid 在某些情况下它是空的。

    这样创建自定义函数是可能的:

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

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

    相关:Multi Select fields in Woocommerce backend


    使用示例(对于一个简单的产品)

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

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

    所选值已正确保存和显示。对于信息,该值是一个数组。

    【讨论】:

    • 感谢您的工作,它有效。我也从您的回答中了解到 woocommerce_wp_checkbox 函数本身不接受超过 1 个选项。
    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2020-11-23
    • 2012-11-20
    相关资源
    最近更新 更多