WooCommerce/WordPress订单完成后自定义邮件及广告优惠券页实现咨询
WooCommerce订单完成后跳转广告页并触发邮件的实现方案
1. 实现订单完成后跳转至自定义页面
在主题的functions.php文件或自定义插件中添加以下代码,通过WooCommerce钩子实现订单完成后的页面跳转:
add_action( 'woocommerce_thankyou', 'redirect_to_custom_promo_page' ); function redirect_to_custom_promo_page( $order_id ) { if ( ! $order_id ) return; $order = wc_get_order( $order_id ); // 仅在订单状态为「已完成」时触发跳转 if ( $order->get_status() === 'completed' ) { // 将订单ID作为参数传递到自定义页面,方便后续获取用户数据 $promo_page_url = add_query_arg( 'order_id', $order_id, get_permalink( get_page_by_title( '广告优惠券页面' ) ) ); wp_safe_redirect( $promo_page_url ); exit; } }
2. 创建带广告/优惠券按钮的自定义页面
在WordPress后台新建页面(标题设为「广告优惠券页面」),使用自定义HTML块添加以下内容:
<div class="promo-wrap"> <h2>感谢您的购买!</h2> <!-- 广告展示区域 --> <div class="ad-block"> <img src="你的广告图片地址" alt="专属推广"> <p>点击下方按钮领取专属购物优惠券</p> </div> <!-- 优惠券领取按钮 --> <button id="get-coupon-btn" data-order-id="<?php echo isset( $_GET['order_id'] ) ? $_GET['order_id'] : ''; ?>">领取优惠券</button> <div id="coupon-notice"></div> </div> <script> jQuery(document).ready(function($) { $('#get-coupon-btn').on('click', function() { const orderId = $(this).data('order-id'); if (!orderId) { $('#coupon-notice').text('无法获取订单信息,请重试'); return; } $.ajax({ url: '<?php echo admin_url('admin-ajax.php'); ?>', type: 'POST', data: { action: 'process_coupon_claim', order_id: orderId }, success: function(res) { if (res.success) { $('#coupon-notice').html('<p>优惠券已发送至您的邮箱,请注意查收!</p>'); $('#get-coupon-btn').prop('disabled', true).text('已领取'); } else { $('#coupon-notice').text('领取失败:' + res.data); } }, error: function() { $('#coupon-notice').text('网络错误,请稍后重试'); } }); }); }); </script>
3. 处理AJAX请求:生成优惠券并发送邮件
继续在functions.php中添加AJAX处理逻辑,实现优惠券生成、用户邮件发送、管理员通知邮件发送:
// 注册AJAX动作(支持登录/未登录用户) add_action( 'wp_ajax_process_coupon_claim', 'handle_coupon_claim_action' ); add_action( 'wp_ajax_nopriv_process_coupon_claim', 'handle_coupon_claim_action' ); function handle_coupon_claim_action() { // 验证订单ID有效性 if ( ! isset( $_POST['order_id'] ) || ! is_numeric( $_POST['order_id'] ) ) { wp_send_json_error( '无效的订单ID' ); } $order_id = intval( $_POST['order_id'] ); $order = wc_get_order( $order_id ); if ( ! $order ) { wp_send_json_error( '订单不存在' ); } // 防止重复领取优惠券 if ( get_post_meta( $order_id, '_coupon_claimed', true ) ) { wp_send_json_error( '您已领取过该优惠券' ); } // 生成随机优惠券码 $coupon_code = 'USER_' . strtoupper( wp_generate_password( 8, false ) ); // 创建优惠券 $coupon = new WC_Coupon(); $coupon->set_code( $coupon_code ); $coupon->set_discount_type( 'fixed_cart' ); // 可选:fixed_cart(固定金额)/percent(百分比) $coupon->set_amount( 25 ); // 优惠券金额,按需修改 $coupon->set_individual_use( true ); $coupon->set_usage_limit( 1 ); $coupon->set_expiry_date( strtotime( '+30 days' ) ); // 30天后过期 $coupon->save(); // 标记订单已领取优惠券 update_post_meta( $order_id, '_coupon_claimed', 'yes' ); // 获取用户及订单数据 $user_email = $order->get_billing_email(); $user_name = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(); $order_total = $order->get_formatted_order_total(); // 发送给用户的优惠券邮件 $user_subject = '您的专属购物优惠券已到账'; $user_content = "您好 {$user_name},\n\n感谢您的购买,您的专属优惠券码是:{$coupon_code}\n\n优惠券可在下次购物时使用,有效期30天。\n\n祝您购物愉快!"; wp_mail( $user_email, $user_subject, $user_content ); // 发送给管理员的通知邮件 $admin_email = get_option( 'admin_email' ); $admin_subject = '用户领取优惠券通知'; $admin_content = "用户 {$user_name}(邮箱:{$user_email})领取了优惠券。\n\n订单ID:{$order_id}\n订单金额:{$order_total}\n优惠券码:{$coupon_code}"; wp_mail( $admin_email, $admin_subject, $admin_content ); wp_send_json_success(); }
4. 额外注意事项
- 建议使用自定义插件添加代码,避免主题更新导致代码丢失
- 可根据需求调整优惠券的折扣类型、金额、有效期等参数
- 广告区域可替换为图片、文字或其他推广内容
- 测试时可手动将订单状态改为「已完成」,验证跳转及邮件功能
内容的提问来源于stack exchange,提问作者pooja T
相关产品推荐
相关产品推荐

