由哪个驱动程序发货的订单[木商订单的钩子已更改,并在管理面板中弹出]

时间:2016-01-03 作者:tom

您好,是否可以在后端创建一个额外字段,当选择订单并将其标记为已发货时,会出现一个额外字段,我可以在其中选择负责此已发货订单的驱动程序。这可能吗?有可用的挂钩吗?

https://wordpress.org/plugins/woocommerce/

Shipped is a custom order status we created.

我的需要是,当订单标记为已发货时,需要显示额外的输入文本或选择框,在其中我可以选择驱动程序名称**(角色为驱动程序的用户)**并保存它。

我可以用wc_order_statuses 滤器请帮忙做这件事。

我知道自定义post meta,它正在运行。但我只想在订单状态更改为已发货时更新该值。这个有没有可用的钩子。当我的员工在批量编辑部分将5,6订单标记为已发货时,需要出现一个弹出窗口,显示“选择驱动程序”,。如何做到这一点

1 个回复
最合适的回答,由SO网友:Prasad Nevase 整理而成

请通读下面的代码。我已经尝试并测试过:)

/**
 * Adds a box to the side column on the Order edit screens. 
 * It checks if the order status is completed & then adds meta box.
 */
function add_drivers_meta_box() {

    global $current_screen;

    /* In the second condistion below, please change it to correct string for \'shipped\' status */
    if ( "shop_order" == $current_screen->post_type && "wc-completed" == get_post_status(get_query_var(\'post\'))) {
        add_meta_box(
            \'shipping_drivers\',
            __( \'Shipped by Driver\', \'myplugin_textdomain\' ),
            \'add_drivers_meta_box_callback\',
            \'shop_order\',
            \'side\'          
        );
    }
}
add_action( \'add_meta_boxes\', \'add_drivers_meta_box\' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function add_drivers_meta_box_callback( $post ) {

    // Add a nonce field so we can check for it later.
    wp_nonce_field( \'add_drivers_save_meta_box_data\', \'add_drivers_meta_box_nonce\' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, \'_shipping_driver_name\', true );

    echo \'<label for="shipping_driver_list">\';
    _e( \'Select Driver Name\', \'myplugin_textdomain\' );
    echo \'</label> \';
    /* Here you need to change the role to \'driver\'. I kept it subscriber because I don\'t have \'driver\' role registered ;-) */
    $drivers = get_users( \'role=subscriber\' );

    echo \'<select id="shipping_driver_list" name="shipping_driver_name" />\';
        foreach ( $drivers as $driver ) {
            $value == $driver->display_name ? $selected = "selected" : $selected = "" ;
            echo \'<option value="\'. esc_html( $driver->display_name ) .\'" \' . $selected .\'>\' . esc_html( $driver->display_name ) . \'</option>\';
        }
    echo \'</select>\';
}

/**
 * When the Order is saved, saves the Driver name.
 *
 * @param int $post_id The ID of the post being saved.
 */
function add_drivers_save_meta_box_data( $post_id ) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST[\'add_drivers_meta_box_nonce\'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'add_drivers_meta_box_nonce\'], \'add_drivers_save_meta_box_data\' ) ) {
        return;
    }

    // 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_page\', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( \'edit_post\', $post_id ) ) {
            return;
        }
    }

    /* OK, it\'s safe for us to save the data now. */

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

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

    // Update the meta field in the database.
    update_post_meta( $post_id, \'_shipping_driver_name\', $my_data );
}
add_action( \'save_post\', \'add_drivers_save_meta_box_data\' );