WooCommerce管理单个订单中带有多个复选框的Metabox [英] Metabox with multi checkbox in WooCommerce admin single orders

查看:57
本文介绍了WooCommerce管理单个订单中带有多个复选框的Metabox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I added successfully a Metabox with a multi checkbox field that is displayed on admin single order pages and works perfectly.

我正在使用 Woocommerce后端中的多个复选框字段该多个复选框的答案代码.

I am using Multi checkbox fields in Woocommerce backend answer code for that multi checkbox.

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

    }
}

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

Final part of code is to save at database, Here is it:

    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 '

I know code works for product pages with action: 'woocommerce_product_process_meta'

因此,我需要保存到db的帮助,这是关于数组的固定错误通知(我认为如果选择默认值,则可能会发生这种情况).

So, i need help for saving at db, an fixing error notice for array (i think this can happen if we select default value).

推荐答案

woowoo_wp_multi_checkbox()函数存在另一个问题,该问题

There was another issue with the function woocommerce_wp_multi_checkbox() that I have updated again (when used in a custom metabox).

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

I have also revisited all your code, specially the last function that saves the multi-checkboxes selected values.

完整代码:

// 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文件中.经过测试,可以正常工作.

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

这篇关于WooCommerce管理单个订单中带有多个复选框的Metabox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆