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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line