如何在Elementor+WooCommerce实现下单后发订单详情至邮箱并免付跳转?
实现WooCommerce免支付下单并发送邮件、自动跳转首页功能
需求说明
- 点击「下单」按钮后无需支付订单费用
- 自动将订单详情与结账详情发送至指定邮箱
- 操作完成后自动重定向至网站首页
- 当前开发环境:Elementor + WooCommerce
实现代码与步骤
1. (可选)新增自定义订单状态
用于标记这类免支付订单,方便后台管理:
// 注册自定义订单状态 add_action('init', 'register_custom_order_status'); function register_custom_order_status() { register_post_status('wc-free-order', array( 'label' => '免支付订单', 'public' => true, 'exclude_from_search' => false, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, 'label_count' => _n_noop('免支付订单 (%s)', '免支付订单 (%s)') )); } // 将自定义状态添加到后台订单状态下拉菜单 add_filter('wc_order_statuses', 'add_custom_order_status_to_dropdown'); function add_custom_order_status_to_dropdown($order_statuses) { $new_order_statuses = array(); foreach ($order_statuses as $key => $status) { $new_order_statuses[$key] = $status; if ('wc-processing' === $key) { $new_order_statuses['wc-free-order'] = '免支付订单'; } } return $new_order_statuses; }
2. 核心处理逻辑:跳过支付、发送邮件、自动跳转
使用WooCommerce钩子拦截订单流程,完成需求功能:
add_action('woocommerce_checkout_order_processed', 'free_order_processing', 10, 3); function free_order_processing($order_id, $posted_data, $order) { // 修改订单状态为自定义状态(也可直接用「已完成」状态) $order->update_status('wc-free-order', '免支付订单,无需支付'); // 配置接收邮箱 $to = '你的邮箱地址@example.com'; $subject = '新免支付订单通知 - 订单号:' . $order->get_order_number(); // 构建邮件内容 $email_content = '<h2>订单详情</h2>'; $email_content .= '<p><strong>订单号:</strong>' . $order->get_order_number() . '</p>'; $email_content .= '<p><strong>下单时间:</strong>' . $order->get_date_created()->format('Y-m-d H:i:s') . '</p>'; // 客户结账信息 $email_content .= '<h3>客户信息</h3>'; $email_content .= '<ul>'; $email_content .= '<li>姓名:' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() . '</li>'; $email_content .= '<li>邮箱:' . $order->get_billing_email() . '</li>'; $email_content .= '<li>电话:' . $order->get_billing_phone() . '</li>'; $email_content .= '<li>地址:' . $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() . ',' . $order->get_billing_city() . ',' . $order->get_billing_state() . ',' . $order->get_billing_postcode() . ',' . $order->get_billing_country() . '</li>'; $email_content .= '</ul>'; // 订单商品列表 $email_content .= '<h3>商品清单</h3>'; $email_content .= '<table border="1" cellpadding="5" cellspacing="0">'; $email_content .= '<tr><th>商品名称</th><th>商品ID</th><th>数量</th><th>单价</th><th>小计</th></tr>'; foreach ($order->get_items() as $item_id => $item) { $product = $item->get_product(); $email_content .= '<tr>'; $email_content .= '<td>' . $item->get_name() . '</td>'; $email_content .= '<td>' . $product->get_id() . '</td>'; $email_content .= '<td>' . $item->get_quantity() . '</td>'; $email_content .= '<td>' . wc_price($product->get_price()) . '</td>'; $email_content .= '<td>' . wc_price($item->get_subtotal()) . '</td>'; $email_content .= '</tr>'; } $email_content .= '</table>'; $email_content .= '<p><strong>订单总金额:</strong>' . wc_price($order->get_total()) . '</p>'; // 发送邮件 $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail($to, $subject, $email_content, $headers); // 自动跳转至网站首页 wp_redirect(home_url()); exit; }
3. (可选)隐藏支付方式
若不需要显示任何支付选项,可添加以下代码:
// 禁用支付需求 add_filter('woocommerce_cart_needs_payment', '__return_false'); // (可选)隐藏条款勾选框 add_filter('woocommerce_checkout_show_terms', '__return_false');
使用说明
- 将上述代码添加到当前主题的
functions.php文件中,或使用自定义代码插件(如Code Snippets) - 替换代码中的
你的邮箱地址@example.com为实际接收邮件的地址 - 测试下单流程,确认邮件发送、订单状态修改及页面跳转功能是否正常
内容的提问来源于stack exchange,提问作者Anas
相关产品推荐
相关产品推荐

