WooCommerce 管理单订单中带有多个复选框的 Metabox

问题描述

我成功添加一个带有多复选框字段的 MetaBox,该字段显示在管理单订单页面上并且运行良好。

我正在为那个多选框使用 Multi checkbox fields in Woocommerce backend 答案代码

 // Adding Meta container admin shop_order pages
add_action( 'add_Meta_Boxes','em_add_Meta_Boxes' );
if ( ! function_exists( 'em_add_Meta_Boxes' ) )
{
    function em_add_Meta_Boxes()
    {
        add_Meta_Box( 'em_other_fields',__('Employee Extra Actions','woocommerce'),'em_add_other_fields_for_order_empl','shop_order','side','core' );
    }
}

// Adding Meta field in the Meta container admin shop_order pages
if ( ! function_exists( 'em_add_other_fields_for_order_empl' ) )
{
    function em_add_other_fields_for_order_empl()
    {
        global $post;

    echo '<div class="options_group">';

        
        woocommerce_wp_multi_checkBox( array(
        'id'    => 'employee_actions12','name'  => 'employee_actions12[]','label' => __('Levels','options' => array(
            'tee'   => __( 'MBO','woocommerce' ),'saa'   => __( 'HBO','tee1'    => __( 'WO',)
    ) );
            

    echo '</div>';

    }
}

代码的最后一部分是保存在数据库中,这里是:

    add_action( 'save_post','save_product_options_custom_fields32',30,1 );
    function save_product_options_custom_fields32( $post_id ){
        if( isset( $_POST['employee_actions12'] ) ){
            $post_data = $_POST['employee_actions12'];
            // Multi 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,'employee_actions12',$sanitize_data );
        }
    }

我知道代码适用于带有操作的产品页面:'woocommerce_product_process_Meta'

所以,我需要帮助保存在数据库中,修复数组的错误通知(我认为如果我们选择认值会发生这种情况)。

解决方法

函数 woocommerce_wp_multi_checkbox() 存在另一个问题,I have updated 再次(在自定义元框中使用时)

我还重新访问了您的所有代码,特别是保存多复选框选定值的最后一个函数。

完整代码:

// WooCommerce admin custom multi checkbox field function
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>';
}

// Adding a custom Metabox on WooCommerce single orders
add_action( 'add_meta_boxes','add_custom_shop_order_metabox' );
function add_custom_shop_order_metabox(){
    add_meta_box(
        'custom_shop_order_metabox',__('Employee Extra Actions','woocommerce'),'content_custom_shop_order_metabox','shop_order','side','core'
    );
}


// Custom Metabox content on WooCommerce single orders
function content_custom_shop_order_metabox() {
    global $thepostid,$post;

    echo '<div class="options_group">';

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


    echo '</div>';
}

// Save WooCommerce single orders Custom Metabox field values
add_action( 'save_post_shop_order','save_custom_shop_order_metabox_field_values' );
function save_custom_shop_order_metabox_field_values( $post_id ){
    if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    || ! current_user_can( 'edit_shop_order',$post_id ) ) {
        return;
    }

    if( isset( $_POST['employee_actions12'] ) ){
        update_post_meta( $post_id,'employee_actions12',wc_clean($_POST['employee_actions12']) );
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。