简体   繁体   English

需要具有自定义 Woocommerce 订单状态的批量状态更改功能

[英]Need Bulk Status Change function with a Custom Woocommerce Order Status

I've used the below php to add an 'imported' custom order status in woocommerce and it works fine我已经使用下面的 php 在 woocommerce 中添加了一个“导入”的自定义订单状态,它工作正常

> **/ function register_imported_order_status() {
>     register_post_status( 'wc-imported', array(
>         'label'                     => 'Imported',
>         'public'                    => true,
>         'exclude_from_search'       => false,
>         'show_in_admin_all_list'    => true,
>         'show_in_admin_status_list' => true,
>         'label_count'               => _n_noop( 'Imported <span class="count">(%s)</span>', 'Imported <span class="count">(%s)</span>'
> )
>     ) ); } add_action( 'init', 'register_imported_order_status' );
> 
> // Add to list of WC Order statuses function
> add_imported_to_order_statuses( $order_statuses ) {
> 
>     $new_order_statuses = array();
> 
>     // add new order status after processing
>     foreach ( $order_statuses as $key => $status ) {
> 
>         $new_order_statuses[ $key ] = $status;
> 
>         if ( 'wc-processing' === $key ) {
>             $new_order_statuses['wc-imported'] = 'Imported';
>         }
>     }
> 
>     return $new_order_statuses; } add_filter( 'wc_order_statuses', 'add_imported_to_order_statuses' );

- ——

However when I try to bulk update the order status - the custom order status does not appear, how can I add the custom status to the bulk actions in Woocommerce Orders.但是,当我尝试批量更新订单状态时 - 未显示自定义订单状态,如何将自定义状态添加到 Woocommerce Orders 中的批量操作

屏幕截图:没有自定义状态批量更新选项

Furthermore, i want the imported status to be considered as having payment captured.此外,我希望将进口状态视为已捕获付款。 Right now the system does not report sales $$$s until we change the status to completed - but we want the sales $$ to be available as soon as they checkout.现在,在我们将状态更改为已完成之前,系统不会报告销售额 $$ - 但我们希望他们结帐后立即可用 $$ 销售额。 How can I make this status include captured payment?我怎样才能使这个状态包括已捕获的付款?

warm regards Jacob温暖的问候雅各布

I built the solution via this guide我通过指南构建了解决方案

The code I use to bulk-edit the 'imported' status in WooCommerce's Orders page is below我用来批量编辑 WooCommerce 订单页面中“已导入”状态的代码如下

<?php
/*
 * Add your custom bulk action in dropdown
 * @since 3.5.0
 */
add_filter( 'bulk_actions-edit-shop_order', 'misha_register_bulk_action' ); // edit-shop_order is the screen ID of the orders page

function misha_register_bulk_action( $bulk_actions ) {

    $bulk_actions['mark_imported'] = 'Mark Imported'; // <option value="mark_imported">Mark Imported</option>
    return $bulk_actions;

}
/*
 * Bulk action handler
 * Make sure that "action name" in the hook is the same like the option value from the above function
 */
add_action( 'admin_action_mark_imported', 'misha_bulk_process_custom_status' ); // admin_action_{action name}

function misha_bulk_process_custom_status() {

    // if an array with order IDs is not presented, exit the function
    if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) )
        return;

    foreach( $_REQUEST['post'] as $order_id ) {

        $order = new WC_Order( $order_id );
        $order_note = 'That\'s what happened by bulk edit:';
        $order->update_status( 'imported', $order_note, true ); // 

    }

    //using add_query_arg() is not required, you can build your URL inline
    $location = add_query_arg( array(
            'post_type' => 'shop_order',
        'marked_imported' => 1, // markED_imported=1 is  the $_GET variable for notices
        'changed' => count( $_REQUEST['post'] ), // number of changed orders
        'ids' => join( $_REQUEST['post'], ',' ),
        'post_status' => 'all'
    ), 'edit.php' );

    wp_redirect( admin_url( $location ) );
    exit;

}

/*
 * Notices
 */
add_action('admin_notices', 'misha_custom_order_status_notices');

function misha_custom_order_status_notices() {

    global $pagenow, $typenow;

    if( $typenow == 'shop_order' 
     && $pagenow == 'edit.php'
     && isset( $_REQUEST['marked_imported'] )
     && $_REQUEST['marked_imported'] == 1
     && isset( $_REQUEST['changed'] ) ) {

        $message = sprintf( _n( 'Order status changed.', '%s order statuses changed.', $_REQUEST['changed'] ), number_format_i18n( $_REQUEST['changed'] ) );
        echo "<div class=\"updated\"><p>{$message}</p></div>";

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM