wordpress 在WooCommerce中执行强制取消说明以改善订单管理

jk9hmnmh  于 2023-10-17  发布在  WordPress
关注(0)|答案(1)|浏览(142)

我正在开发一个系统,其中必须包括一个订单说明,解释我的电子商务网站上取消订单的原因。我观察到由于各种原因被取消的订单数量持续增加,我的一些团队成员没有详细说明,这阻碍了我们有效解决这些问题的能力。
我实现了一个代码解决方案,旨在强制用户输入以“order got cancelled because”开头的订单备注。目的是仅当订单备注中存在此特定字符串时,才触发订单状态更改为“cancelled”。如果没有提供所需的注解,系统应自动将订单状态恢复到以前的状态。
然而,我遇到了一个问题,即使没有添加相关说明,订单也被标记为“取消”。尽管我做了努力,这个问题仍然存在,我正在寻求帮助来解决这个问题。下面是我实现的代码:

add_action('woocommerce_order_status_changed', 'check_order_status_change', 10, 4);
function check_order_status_change($order_id, $from_status, $to_status, $order) {
    // Check if the new status is "cancelled"
    if ($to_status === 'cancelled') {
        // Get all order notes
        $order_notes = $order->get_customer_order_notes();

        $cancelled_note_exists = false;

        // Check if there's an order note starting with "order got cancelled because"
        foreach ($order_notes as $note) {
            if (strpos($note->content, 'order got cancelled because') === 0) {
                $cancelled_note_exists = true;
                break;
            }
        }

        // If a cancellation reason note does not exist, revert the order status
        if (!$cancelled_note_exists) {
            // Get the previous status from post meta
            $previous_status = get_post_meta($order_id, '_status_previous', true);

            if (!empty($previous_status)) {
                // Update the order status
                $order->set_status($previous_status);
                $order->save();

                // Add an admin note indicating the status change
                $order->add_order_note(__('Order status reverted to ' . $previous_status . ' because a cancellation reason was not provided.', 'woocommerce'), true);
            }
        }
    }
}
1szpjjfi

1szpjjfi1#

请尝试以下操作,如果没有包含“order got cancelled because”字符串的取消原因的订单备注,则将停止将状态更新为取消(它还将显示警告备注):

add_action( 'woocommerce_before_order_object_save', 'process_before_order_object_save',  10, 2 );
function process_before_order_object_save( $order, $data_store ) {
    global $pagenow;
    // Find out if order status change is "cancelled"
    if ( is_admin() && $pagenow === 'post.php' && $order->get_status() === 'cancelled' ) {
        $string_to_find = "order got cancelled because";
        $note_found     = false; // Initializing   
    
        // Loop through order notes
        foreach(wc_get_order_notes(['order_id' => $order->get_id()]) as $order_note ) {
            // Check if there is a note that contains "order got cancelled because"
            if ( strpos($order_note->content, $string_to_find) !== false ) {
                $note_found = true;
                break;
            }
        }
        // If no note found stop status change process
        if ( ! $note_found ) {
            throw new Exception( "You are not allowed to cancel order without adding a note before with a valid cancellation reason." );
            wp_die();
        }
    }
}

代码放在子主题的functions.php文件中(或插件中)。测试和作品。
您还需要从管理订单中删除“mark_cancelled”批量操作:

add_filter( 'bulk_actions-edit-shop_order', 'remove_mark_cancelled_bulk_action', 100 );
function remove_mark_cancelled_bulk_action( $bulk_actions ) {
    foreach ($bulk_actions as $key => $action) {
        if ( 'mark_cancelled' === $key ) {
            unset($bulk_actions[$key]);
        }
    }
    return $bulk_actions;
}

相关问题