You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现WooCommerce custom order request审批及自动转订单功能

WooCommerce自定义订单申请功能实现方案

整体实现分为自定义申请存储、前端表单、后台审批、邮件通知4个核心模块,所有逻辑可通过代码集成到主题或自定义插件中,无需依赖额外第三方工具。

1. 自定义申请存储结构

首先注册独立的自定义文章类型存储客户提交的申请,避免和原有订单、文章数据混淆:

// 注册自定义文章类型:订单申请
function register_order_request_cpt() {
    $args = array(
        'public' => true,
        'label'  => '订单申请',
        'supports' => array('title', 'editor'),
        'menu_icon' => 'dashicons-list-view',
        'capability_type' => 'post',
    );
    register_post_type('wc_order_request', $args);
}
add_action('init', 'register_order_request_cpt');

注册后后台会生成独立的「订单申请」菜单,所有客户提交的申请都会存在这里。

2. 前端表单实现

通过短代码生成提交表单,可插入到站点任意位置(页面、文章、侧边栏小工具都支持):

// 前端提交表单短代码
function order_request_form_shortcode() {
    if (isset($_POST['submit_order_request']) && wp_verify_nonce($_POST['order_request_nonce'], 'submit_order_request')) {
        // 验证数据后插入申请记录
        $post_id = wp_insert_post(array(
            'post_type' => 'wc_order_request',
            'post_title' => sanitize_text_field($_POST['request_title']),
            'post_content' => sanitize_textarea_field($_POST['request_content']),
            'post_status' => 'pending',
        ));
        // 存储客户自定义字段
        update_post_meta($post_id, 'customer_name', sanitize_text_field($_POST['customer_name']));
        update_post_meta($post_id, 'customer_email', sanitize_email($_POST['customer_email']));
        update_post_meta($post_id, 'customer_phone', sanitize_text_field($_POST['customer_phone']));
        
        echo '<p>申请提交成功,我们会尽快处理并通知您。</p>';
    }
    // 渲染表单HTML
    ob_start(); ?>
    <form method="post">
        <?php wp_nonce_field('submit_order_request', 'order_request_nonce'); ?>
        <p>
            <label>姓名 *</label>
            <input type="text" name="customer_name" required>
        </p>
        <p>
            <label>邮箱 *</label>
            <input type="email" name="customer_email" required>
        </p>
        <p>
            <label>联系电话</label>
            <input type="tel" name="customer_phone">
        </p>
        <p>
            <label>申请标题 *</label>
            <input type="text" name="request_title" required>
        </p>
        <p>
            <label>需求详情 *</label>
            <textarea name="request_content" required rows="5"></textarea>
        </p>
        <p><input type="submit" name="submit_order_request" value="提交申请"></p>
    </form>
    <?php
    return ob_get_clean();
}
add_shortcode('order_request_form', 'order_request_form_shortcode');

使用时只需在需要展示表单的位置插入短代码[order_request_form]即可。

3. 后台审批功能实现

在申请列表添加审批操作按钮,处理批准/驳回的业务逻辑:

// 后台列表添加审批按钮
function add_order_request_action_buttons($actions, $post) {
    if ($post->post_type == 'wc_order_request' && $post->post_status == 'pending') {
        $actions['approve'] = '<a href="' . admin_url('admin.php?action=approve_request&request_id=' . $post->ID) . '">批准</a>';
        $actions['reject'] = '<a href="' . admin_url('admin.php?action=reject_request&request_id=' . $post->ID) . '">驳回</a>';
    }
    return $actions;
}
add_filter('post_row_actions', 'add_order_request_action_buttons', 10, 2);

// 处理审批逻辑
function handle_order_request_approval() {
    if (isset($_GET['action']) && isset($_GET['request_id']) && current_user_can('manage_woocommerce')) {
        $request_id = absint($_GET['request_id']);
        $customer_email = get_post_meta($request_id, 'customer_email', true);
        $customer_name = get_post_meta($request_id, 'customer_name', true);
        
        if ($_GET['action'] == 'approve_request') {
            // 批准后生成正式WooCommerce订单
            $order = wc_create_order();
            $order->set_billing_first_name($customer_name);
            $order->set_billing_email($customer_email);
            $order->set_billing_phone(get_post_meta($request_id, 'customer_phone', true));
            // 可根据需求添加商品、金额、运费等信息
            $order->add_fee('自定义订单需求', 0); 
            $order->set_status('processing');
            $order->save();
            
            // 更新申请状态,关联生成的订单
            wp_update_post(array('ID' => $request_id, 'post_status' => 'publish'));
            update_post_meta($request_id, 'related_order_id', $order->get_id());
            
            // 发送批准通知
            $to = $customer_email;
            $subject = '您的订单申请已批准';
            $message = '您好,您提交的订单申请已通过审批,正式订单号为:' . $order->get_order_number() . ',我们会尽快为您处理。';
            wp_mail($to, $subject, $message);
            
            wp_redirect(admin_url('edit.php?post_type=wc_order_request&status=approved'));
            exit;
        } elseif ($_GET['action'] == 'reject_request') {
            // 驳回逻辑
            wp_update_post(array('ID' => $request_id, 'post_status' => 'draft'));
            
            $to = $customer_email;
            $subject = '您的订单申请已驳回';
            $message = '您好,很抱歉您提交的订单申请未通过审批,如有疑问可联系我们的客服。';
            wp_mail($to, $subject, $message);
            
            wp_redirect(admin_url('edit.php?post_type=wc_order_request&status=rejected'));
            exit;
        }
    }
}
add_action('admin_init', 'handle_order_request_approval');

注意事项

  • 以上代码可放到主题的functions.php文件中,或者自定义插件中,避免主题更新后代码丢失
  • 表单字段可根据业务需求自行增减,对应修改数据存储的逻辑即可
  • 订单创建时的商品、金额逻辑可根据实际需求调整,示例中仅做基础演示
  • 邮件内容、发件人信息可通过wp_mail相关钩子自定义,也可以复用WooCommerce自带的邮件模板保持风格统一
  • 生产环境使用时建议添加表单验证、提交频率限制,防止垃圾提交

内容的提问来源于stack exchange,提问作者Md Nowshad Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 09:09:00