Metabox具有WooCommerce管理订单页面的多个自定义字段 [英] Metabox with multiple custom fields for WooCommerce admin order pages

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

问题描述

前几天,我找到了以下代码,现在我的客户想要另外两个字段.如果我再次将其添加到代码段插件中,则会显示无法重新声明函数set_custom_edit_shop_order_columns.".有人可以帮我用另外两个自定义字段来更改代码吗?

I found the below code the other day, and now my client wants two more fields. If I add it again to my snippet plugin it says "Cannot redeclare function set_custom_edit_shop_order_columns.". Could someone kindly help me to change the code with two more custom fields?

我已将custom_column更改为courier_reference,并希望另一个文本字段tracking_number,还有一个是带有日期选择器而不是文本框的shipping_date.

I have changed custom_column to courier_reference and want another text field tracking_number and one more is shipping_date with a date picker rather than a text box.

感谢您的帮助.

//from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

// For displaying in columns.

add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
    $columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
    return $columns;
}

// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
    switch ( $column ) {

        case 'custom_column' :
            echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
            break;

    }
}

// For display and saving in order details page.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {

    add_meta_box(
        'custom_column',
        __( 'Custom Column', 'your_text_domain' ),
        'shop_order_display_callback',
        'shop_order'
    );

}

// For displaying.
function shop_order_display_callback( $post ) {

    $value = get_post_meta( $post->ID, 'custom_column', true );

    echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
}

// For saving.
function save_shop_order_meta_box_data( $post_id ) {

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
            return;
        }
    }

    // Make sure that it is set.
    if ( ! isset( $_POST['custom_column'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST['custom_column'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, 'custom_column', $my_data );
}

add_action( 'save_post', 'save_shop_order_meta_box_data' );

推荐答案

已更新... 您只需要在同一函数中具有多个不同的子段.对于所需的3个自定义字段,请使用以下内容:

Updated... You just need to have multiple different slugs in the same functions. For your desired 3 custom fields use the following:

// Add columns to admin orders table.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
    $columns['courier_reference'] = __( 'Courier reference', 'woocommerce' );
    $columns['tracking_number'] = __( 'Tracking number', 'woocommerce' );
    $columns['shipping_date'] = __( 'Shipping date', 'woocommerce' );
    return $columns;
}

// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
    switch ( $column ) {

        case 'courier_reference' :
            echo esc_html( get_post_meta( $post_id, 'courier_reference', true ) );
            break;

        case 'tracking_number' :
            echo esc_html( get_post_meta( $post_id, 'tracking_number', true ) );
            break;

        case 'shipping_date' :
            echo esc_html( get_post_meta( $post_id, 'shipping_date', true ) );
            break;

    }
}

// Add a metabox.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {

    add_meta_box(
        'custom_meta_box',
        __( 'Tracking information', 'woocommerce' ),
        'shop_order_content_callback',
        'shop_order'
    );

}

// For displaying metabox content
function shop_order_content_callback( $post ) {

    // Textarea Field
    $courier_reference = get_post_meta( $post->ID, 'courier_reference', true );

    echo '<p>' . __( 'Courier reference', 'woocommerce' ) . '<br>
    <textarea style="width:100%" id="courier_reference" name="courier_reference">' . esc_attr( $courier_reference ) . '</textarea></p>';

    // Text field
    $tracking_number = get_post_meta( $post->ID, 'tracking_number', true );
    echo '<p>' . __( 'Tracking number', 'woocommerce' ) . '<br>
    <input type="text" style="width:100%" id="tracking_number" name="tracking_number" value="' . esc_attr( $tracking_number ) . '"></p>';

    // Date picker field
    $shipping_date = get_post_meta( $post->ID, 'shipping_date', true );

    echo '<p>' . __( 'shipping_date', 'woocommerce' ) . '<br>
    <input type="date" style="width:100%" id="shipping_date" name="shipping_date" value="' . esc_attr( $shipping_date ) . '"></p>';
}

// For saving the metabox data.
add_action( 'save_post_shop_order', 'save_shop_order_meta_box_data' );
function save_shop_order_meta_box_data( $post_id ) {

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
        return;
    }


    // Make sure that 'shipping_date' is set.
    if ( isset( $_POST['courier_reference'] ) ) {

        // Update the meta field in the database.
        update_post_meta( $post_id, 'courier_reference', sanitize_textarea_field( $_POST['courier_reference'] ) );
    }

    // Make sure that 'tracking_number' it is set.
    if ( isset( $_POST['tracking_number'] ) ) {

        // Update the meta field in the database.
        update_post_meta( $post_id, 'tracking_number', sanitize_text_field( $_POST['tracking_number'] ) );
    }

    // Make sure that 'shipping_date' is set.
    if ( isset( $_POST['shipping_date'] ) ) {

        // Update the meta field in the database.
        update_post_meta( $post_id, 'shipping_date', sanitize_text_field( $_POST['shipping_date'] ) );
    }
}

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

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

这篇关于Metabox具有WooCommerce管理订单页面的多个自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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