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

Metabox with multi checkbox in WooCommerce admin single orders

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

我正在为那个多复选框使用 答案代码。

 // 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', 'woocommerce'),
        'options' => array(
            'tee'   => __( 'MBO', 'woocommerce' ),
            'saa'   => __( 'HBO', 'woocommerce' ),
            'tee1'    => __( 'WO', 'woocommerce' ),
        )
    ) );
            

    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'

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

函数 woocommerce_wp_multi_checkbox() 存在另一个问题 再次 (在自定义元数据框中使用时).

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

完整代码:

// 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', 'woocommerce'),
        'options' => array(
            'tee'  => __( 'MBO', 'woocommerce' ),
            'saa'  => __( 'HBO', 'woocommerce' ),
            'tee1' => __( 'WO', 'woocommerce' ),
        ),
    ) );


    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 文件。已测试并有效。