May 31, 2026 Uncategorized

WooCommerce定制开发案例:外卖订餐系统完整方案

项目背景

客户是一家餐饮企业,希望基于WooCommerce开发在线外卖订餐系统。需求包括:菜品展示、购物车、配送地址选择、配送时间选择、在线支付、订单实时追踪等。

这是一个典型的O2O(Online to Offline) WooCommerce二次开发项目,涉及复杂的配送逻辑和实时订单管理。

客户痛点

  • 电话订餐效率低:高峰期电话占线,客户流失
  • 配送范围限制:需要限制配送范围(如3公里内)
  • 配送时间选择:客户需要选择期望送达时间
  • 支付与订单同步:支付成功后需要自动打印订单小票
  • 实时订单追踪:客户希望看到订单状态(制作中、配送中、已送达)

我们的解决方案

1. 菜品展示与购物车

  • 使用WooCommerce产品系统管理菜品
  • 为菜品添加自定义属性(如辣度、温度、配料等)
  • 使用Ajax购物车,无需刷新页面即可添加菜品
  • 使用WooCommerce Product Add-ons插件实现菜品定制选项

2. 配送地址与范围限制

  • 使用高德地图API百度地图API获取用户地址坐标
  • 计算餐厅与用户的距离,如果超出配送范围则禁止下单
  • 使用WordPress会话(Session)存储用户地址信息
  • 后台可设置配送范围半径(如3公里、5公里)

3. 配送时间选择

  • 用户下单时选择期望送达时间(如”尽快送达”或”指定时间”)
  • 使用jQuery UI Timepicker实现时间选择
  • 后台可设置营业时间配送时段
  • 使用WooCommerce Checkout Fields Editor插件添加时间选择字段

4. 在线支付与订单打印

  • 集成微信支付支付宝PayPal等支付方式
  • 支付成功后,使用小票打印机API(如飞鹅打印机、易联云打印机)自动打印订单
  • 使用WooCommerce REST API处理支付回调
  • 使用WordPress Cron定时检查打印状态,失败则重试

5. 实时订单追踪

  • 使用WooCommerce自定义订单状态(如”制作中”、”配送中”、”已送达”)
  • 用户中心可实时查看订单状态
  • 使用Google Maps API高德地图API展示配送员实时位置
  • 使用WebSocket长轮询实现订单状态实时推送

技术实现细节

核心代码片段

// 配送范围检查(根据经纬度计算距离)add_action('woocommerce_checkout_process', 'validate_delivery_distance');function validate_delivery_distance() {  $user_lat = floatval($_POST['shipping_latitude']);  $user_lng = floatval($_POST['shipping_longitude']);    // 餐厅经纬度(从后台设置读取)  $restaurant_lat = floatval(get_option('restaurant_latitude'));  $restaurant_lng = floatval(get_option('restaurant_longitude'));    // 计算距离(使用Haversine公式)  $distance = haversine_distance($restaurant_lat, $restaurant_lng, $user_lat, $user_lng);    // 获取配送范围设置  $delivery_radius = floatval(get_option('delivery_radius', 3)); // 默认3公里    if ($distance > $delivery_radius) {    wc_add_notice(sprintf('抱歉,您的地址超出配送范围(%.1f公里)。我们的配送范围为餐厅周边%.1f公里。', $distance, $delivery_radius), 'error');  }}// Haversine公式:计算两个经纬度之间的距离function haversine_distance($lat1, $lng1, $lat2, $lng2) {  $earth_radius = 6371; // 地球半径(公里)    $dlat = deg2rad($lat2 - $lat1);  $dlng = deg2rad($lng2 - $lng1);    $a = sin($dlat/2) * sin($dlat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dlng/2) * sin($dlng/2);  $c = 2 * atan2(sqrt($a), sqrt(1-$a));    return $earth_radius * $c;}// 支付成功后打印订单小票add_action('woocommerce_payment_complete', 'print_order_receipt');function print_order_receipt($order_id) {  $order = wc_get_order($order_id);    // 获取打印机API配置  $printer_api_url = get_option('printer_api_url');  $printer_api_key = get_option('printer_api_key');    // 构造小票内容  $receipt_content = build_receipt_content($order);    // 调用打印机API  $response = wp_remote_post($printer_api_url, [    'headers' => [      'Authorization' => 'Bearer ' . $printer_api_key,      'Content-Type' => 'application/json',    ],    'body' => json_encode([      'content' => $receipt_content,    ]),  ]);    if (is_wp_error($response)) {    // 打印失败,记录日志并安排重试    error_log('打印订单小票失败:订单ID ' . $order_id);    wp_schedule_single_event(time() + 60, 'retry_print_order_receipt', [$order_id]);  }}// 构建小票内容function build_receipt_content($order) {  $content = "========== 订单小票 ==========\n";  $content .= "订单号:" . $order->get_order_number() . "\n";  $content .= "下单时间:" . $order->get_date_created()->date('Y-m-d H:i:s') . "\n";  $content .= "配送地址:" . $order->get_shipping_address_1() . "\n";  $content .= "联系电话:" . $order->get_billing_phone() . "\n";  $content .= "========== 菜品明细 ==========\n";    foreach ($order->get_items() as $item) {    $product = $item->get_product();    $content .= $item->get_name() . ' x ' . $item->get_quantity() . '  ' . wc_price($item->get_total()) . "\n";        // 打印菜品定制选项(如辣度、温度等)    $meta_data = $item->get_formatted_meta_data();    if (!empty($meta_data)) {      foreach ($meta_data as $meta) {        $content .= '  - ' . $meta->display_key . ':' . $meta->display_value . "\n";      }    }  }    $content .= "========== 费用明细 ==========\n";  $content .= "菜品总额:" . wc_price($order->get_subtotal()) . "\n";  $content .= "配送费:" . wc_price($order->get_shipping_total()) . "\n";  $content .= "优惠折扣:" . wc_price($order->get_discount_total()) . "\n";  $content .= "订单总计:" . wc_price($order->get_total()) . "\n";  $content .= "========== 感谢惠顾 ==========\n";    return $content;}

项目成果

  • 订单量提升:在线订餐系统上线后,订单量提升200%
  • 人工成本降低:电话订餐人工成本降低70%
  • 配送范围控制精准:超出配送范围的订单占比从15%降到0%
  • 客户满意度提升:实时订单追踪功能上线后,客户满意度提升40%
  • 订单打印稳定:支付成功后小票打印成功率达到99.5%

项目周期与报价

  • 开发周期60天(需求分析10天 + 开发45天 + 测试5天)
  • 项目报价¥35,000(含需求分析、开发、测试、上线、3个月维护)
  • 技术方案:WordPress + WooCommerce + 地图API + 支付接口 + 打印机API

结语

这个案例展示了WooCommerce在O2O外卖订餐领域的强大扩展能力。通过二次开发,WooCommerce不仅可以做传统电商,还可以做外卖订餐、服务预约、配送管理等复杂业务场景。

如果您也需要基于WooCommerce开发外卖订餐系统,或者需要定制其他WooCommerce功能,请联系我们!我们拥有丰富的WooCommerce二次开发经验,可以为您提供从需求分析、方案设计到开发上线的全流程服务。

服务内容包括

  • WooCommerce功能定制开发
  • O2O外卖订餐系统开发
  • 地图API集成(高德、百度、Google Maps)
  • 支付接口对接(微信支付、支付宝、PayPal等)
  • 小票打印机API对接
  • 实时订单追踪系统开发

立即联系我们,获取免费技术方案和报价!

Leave a Reply

Your email address will not be published. Required fields are marked *