dqkyz02602 2019-07-06 04:53
浏览 95
已采纳

在WooCommerce管理订单列表中的批量操作中添加自定义取消订单状态

I want to cancel all selected order on WooCommerce in the admin order list.

I have written the code but it's not working

add_filter( 'bulk_actions-edit-shop_order', 'my_register_bulk_action' ); // edit-shop_order is the screen ID of the orders page

function my_register_bulk_action( $bulk_actions ) {

$bulk_actions['mark_change_status_to_cancelled'] = 'Order Cancel'; // <option value="mark_awaiting_shipment">Order Cancel</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_change_status_to_cancelled', 'my_bulk_process_custom_status' ); // admin_action_{action name}

function my_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( 'Cancelled', $order_note, true ); // "my-shipment" is the order status name 
}

// of course using add_query_arg() is not required, you can build your URL inline
$location = add_query_arg( array(
        'post_type' => 'shop_order',
    'mark_change_status_to_cancelled' => 1, // mark_change_status_to_cancelled=1 is just 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', 'my_custom_order_status_notices');


function my_custom_order_status_notices() {

    global $pagenow, $typenow;


if( $typenow == 'shop_order' 
 && $pagenow == 'edit.php'
 && isset( $_REQUEST['mark_change_status_to_cancelled'] )
 && $_REQUEST['mark_change_status_to_cancelled'] == 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>";

}


}

http://prntscr.com/oba9vg

Now it's showing Cancel Order under the bulk actions but unfortunately, function doesn't cancel the orders. what should I do?

  • 写回答

1条回答 默认 最新

  • dongyouzhi7218 2019-08-24 17:21
    关注

    For this section:

    foreach( $_REQUEST['post'] as $order_id ) {
    
        $order = new WC_Order( $order_id );
        $order_note = 'That\'s what happened by bulk edit:';
        $order->update_status( 'Cancelled', $order_note, true ); // "my-shipment" is the order status name 
    }
    

    Please use "cancelled" instead of "Cancelled":

    $order->update_status( 'cancelled', $order_note, true );
    

    I think, then it'll work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏