WooCommerce个性化优惠券优化:添加至订单详情、账户及邮件
解决方案:实现WooCommerce订单完成后优惠券的多渠道展示与邮件推送
我来帮你搞定这个需求!你的核心问题是没有把优惠券信息和订单本身绑定存储,导致无法在账户页面和邮件中调取。我们需要对现有代码做三处关键修改:将优惠券信息存入订单元数据、在用户账户的订单模块展示、以及自动推送邮件。下面是完整的实现方案:
1. 修改优惠券生成逻辑:绑定到订单元数据
首先,我们需要在生成优惠券后,把优惠券代码和ID存储到对应订单的元数据中,这样后续才能从订单中读取这些信息。修改原来的create_automatic_coupon_only_for_this_customer函数,在生成优惠券后添加同步订单元数据的代码:
// 生成优惠券后,将优惠券信息保存到订单元数据 if ( !$coupon_id ) { // ... 原来的优惠券生成代码 ... // 添加这两行:保存优惠券代码和ID到订单 $order->update_meta_data( '_customer_reward_coupon_code', $coupon_code ); $order->update_meta_data( '_customer_reward_coupon_id', $new_coupon_id ); $order->save(); } else { // 如果已经存在优惠券,也同步到订单元数据(防止重复生成) $order->update_meta_data( '_customer_reward_coupon_code', $coupon_code ); $order->update_meta_data( '_customer_reward_coupon_id', $coupon_id ); $order->save(); }
2. 在订单详情中添加自定义字段
我们需要在后台订单详情页和前台用户订单详情页都显示优惠券信息:
后台订单详情显示
添加以下代码,在后台订单的账单信息下方展示优惠券:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_coupon_in_admin_order_details', 10, 1 ); function display_coupon_in_admin_order_details( $order ) { $coupon_code = $order->get_meta( '_customer_reward_coupon_code' ); if ( !empty( $coupon_code ) ) { $expiry_date = get_post_meta( $order->get_meta( '_customer_reward_coupon_id' ), 'expiry_date', true ); echo '<div class="address"> <p><strong>' . __( 'Reward Coupon', 'woocommerce' ) . ':</strong> ' . $coupon_code . '</p> <p><strong>' . __( 'Expires On', 'woocommerce' ) . ':</strong> ' . $expiry_date . '</p> </div>'; } }
前台用户订单详情显示
添加以下代码,在用户前台订单详情页的订单表格下方展示优惠券:
add_action( 'woocommerce_order_details_after_order_table', 'display_coupon_in_frontend_order_details', 10, 1 ); function display_coupon_in_frontend_order_details( $order ) { $coupon_code = $order->get_meta( '_customer_reward_coupon_code' ); if ( !empty( $coupon_code ) ) { $coupon_id = $order->get_meta( '_customer_reward_coupon_id' ); $expiry_date = get_post_meta( $coupon_id, 'expiry_date', true ); $amount = get_post_meta( $coupon_id, 'coupon_amount', true ); $discount_type = get_post_meta( $coupon_id, 'discount_type', true ); $discount = $amount . ( 'percent' == $discount_type ? '%' : get_woocommerce_currency() ); echo '<div style="margin: 2em 0; padding: 1em; border: 1px dashed #ccc; text-align:center;"> <h3>' . __( 'Your Exclusive Reward Coupon', 'woocommerce' ) . '</h3> <p>' . __( 'Thank you for your purchase! Here\'s your exclusive discount:', 'woocommerce' ) . '</p> <div style="font-size:1.5em; font-weight:bold; margin:1em 0;">' . $coupon_code . '</div> <p>' . __( 'Discount:', 'woocommerce' ) . ' ' . $discount . '</p> <p>' . __( 'Expires On:', 'woocommerce' ) . ' ' . $expiry_date . '</p> </div>'; } }
3. 在「我的账户」订单板块展示优惠券
我们可以在用户账户的订单列表中添加一列显示优惠券,方便用户快速查看:
// 添加自定义列 add_filter( 'woocommerce_my_account_my_orders_columns', 'add_coupon_column_to_my_orders' ); function add_coupon_column_to_my_orders( $columns ) { $columns['reward_coupon'] = __( 'Reward Coupon', 'woocommerce' ); return $columns; } // 填充列内容 add_action( 'woocommerce_my_account_my_orders_column_reward_coupon', 'fill_coupon_column_in_my_orders', 10, 1 ); function fill_coupon_column_in_my_orders( $order ) { $coupon_code = $order->get_meta( '_customer_reward_coupon_code' ); echo !empty( $coupon_code ) ? $coupon_code : '-'; }
4. 订单完成时自动发送优惠券邮件
我们需要在WooCommerce的订单完成邮件中插入优惠券信息,支持HTML和纯文本两种格式:
add_action( 'woocommerce_email_order_details', 'add_coupon_to_completion_email', 20, 4 ); function add_coupon_to_completion_email( $order, $sent_to_admin, $plain_text, $email ) { // 只在订单完成邮件中发送给客户 if ( $email->id == 'customer_completed_order' && !$sent_to_admin ) { $coupon_code = $order->get_meta( '_customer_reward_coupon_code' ); if ( !empty( $coupon_code ) ) { $coupon_id = $order->get_meta( '_customer_reward_coupon_id' ); $expiry_date = get_post_meta( $coupon_id, 'expiry_date', true ); $amount = get_post_meta( $coupon_id, 'coupon_amount', true ); $discount_type = get_post_meta( $coupon_id, 'discount_type', true ); $discount = $amount . ( 'percent' == $discount_type ? '%' : get_woocommerce_currency() ); if ( $plain_text ) { // 纯文本邮件格式 echo "\n\n" . __( 'Your Exclusive Reward Coupon', 'woocommerce' ) . ":\n"; echo $coupon_code . "\n"; echo __( 'Discount:', 'woocommerce' ) . ' ' . $discount . "\n"; echo __( 'Expires On:', 'woocommerce' ) . ' ' . $expiry_date . "\n"; } else { // HTML邮件格式 echo '<div style="margin: 2em 0; padding: 1em; border: 1px dashed #ccc; text-align:center;"> <h3>' . __( 'Your Exclusive Reward Coupon', 'woocommerce' ) . '</h3> <p>' . __( 'Thank you for your purchase! Here\'s your exclusive discount:', 'woocommerce' ) . '</p> <div style="font-size:1.5em; font-weight:bold; margin:1em 0;">' . $coupon_code . '</div> <p>' . __( 'Discount:', 'woocommerce' ) . ' ' . $discount . '</p> <p>' . __( 'Expires On:', 'woocommerce' ) . ' ' . $expiry_date . '</p> </div>'; } } } }
完整整合后的代码
把以上所有代码和你原来的代码整合起来,最终代码如下:
// Create a unique discount coupon for each customer at the end of their purchase if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ){ add_filter( 'woocommerce_thankyou_order_received_text', 'create_automatic_coupon_only_for_this_customer', 99, 2 ); function create_automatic_coupon_only_for_this_customer( $message, $order ){ // Coupon configuration $discount_type = 'percent'; // 'percent' 或 'fixed_cart' $amount = 10; $expiration = 180; // 清理过期优惠券并检查订单是否已有优惠券 $coupon_id = clean_expired_coupons_and_check_coupons_order( $order ); $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) ); if ( $order->get_id() > 0 && $order->get_order_key() == $order_key ) { if ( !$coupon_id ) { // 创建优惠券 $customer_email = $order->get_billing_email(); $customer_email = explode( '@', $customer_email ); $customer_email = $customer_email[0]; $suffix = date( 'Hms' ); $date = date( 'Y-m-d' ); $expiry_date = strtotime( $date .'+ '. $expiration .' days' ); $expiry_date = date( 'Y-m-d', $expiry_date ); $coupon_code = strtoupper( $customer_email.'_'.$suffix ); $coupon = array( 'post_title' => $coupon_code, 'post_content' => '', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'shop_coupon' ); $new_coupon_id = wp_insert_post( $coupon ); // 设置优惠券元数据 update_post_meta( $new_coupon_id, 'discount_type', $discount_type ); update_post_meta( $new_coupon_id, 'coupon_amount', $amount ); update_post_meta( $new_coupon_id, 'individual_use', 'yes' ); update_post_meta( $new_coupon_id, 'usage_limit', 1 ); update_post_meta( $new_coupon_id, 'expiry_date', $expiry_date ); update_post_meta( $new_coupon_id, 'order-id', $order->get_id() ); if ( is_wp_error( $new_coupon_id ) ){ return false; } // 保存优惠券信息到订单元数据 $order->update_meta_data( '_customer_reward_coupon_code', $coupon_code ); $order->update_meta_data( '_customer_reward_coupon_id', $new_coupon_id ); $order->save(); }else{ $coupon = get_post( $coupon_id ); $coupon_code = $coupon->post_title; // 同步已有优惠券信息到订单元数据 $order->update_meta_data( '_customer_reward_coupon_code', $coupon_code ); $order->update_meta_data( '_customer_reward_coupon_id', $coupon_id ); $order->save(); } // 在感谢页展示优惠券 $message .= get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type ); } return $message; } function get_coupon_discount_for_the_next_purchase( $coupon_code, $expiration, $amount, $discount_type ){ $discount = $amount; $discount .= 'percent' == $discount_type? '%' : get_woocommerce_currency(); ob_start(); ?> <div style="margin: 0 0 3.5em; text-align:center;"> <p>To thank you for your trust in us, we have prepared a <strong> discount coupon for you <?php echo $discount; ?> for your next purchase:</strong></p> <h3 style="text-align:center;">YOUR DISCOUNT</h3> <div style="text-align: center; font-weight: bold; border: 2px #c7c7c7; border-style: dashed; padding: 13px; width: 50%; margin: 10px auto;"><?php echo $coupon_code;?></div> <p><strong>The coupon is valid for de <?php echo $expiration; ?> Days</strong>. But do not leave it too much because once that period has elapsed, the coupon will no longer be valid.</p> </div> <?php $content = ob_get_contents(); ob_end_clean(); return $content; } function clean_expired_coupons_and_check_coupons_order( $order = false ){ $coupon_exist = false; if ( isset( $order ) ) { $arg = array( 'post_type' => array( 'shop_coupon' ), 'meta_key' => 'order-id' ); $auto_coupon_list = new WP_Query( $arg ); foreach ( $auto_coupon_list->posts as $key => $coupon ) { // 删除过期优惠券 $coupon_expiry_date = get_post_meta( $coupon->ID, 'expiry_date', true ); $today_date = date( 'Y-m-d' ); if ( !empty( $coupon_expiry_date ) && ( $today_date > $coupon_expiry_date ) ) { wp_delete_post( $coupon->ID ); continue; } // 查找关联当前订单的优惠券 $order_id_coupon_vinculated = get_post_meta( $coupon->ID, 'order-id', true ); if ( !$coupon_exist && ( $order_id_coupon_vinculated == $order->get_id() ) ) { $coupon_exist = $coupon->ID; } } } return $coupon_exist; } // 后台订单详情显示优惠券 add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_coupon_in_admin_order_details', 10, 1 ); function display_coupon_in_admin_order_details( $order ) { $coupon_code = $order->get_meta( '_customer_reward_coupon_code' ); if ( !empty( $coupon_code ) ) { $expiry_date = get_post_meta( $order->get_meta( '_customer_reward_coupon_id' ), 'expiry_date', true ); echo '<div class="address"> <p><strong>' . __( 'Reward Coupon', 'woocommerce' ) . ':</strong> ' . $coupon_code . '</p> <p><strong>' . __( 'Expires On', 'woocommerce' ) . ':</strong> ' . $expiry_date . '</p> </div>'; } } // 前台用户订单详情显示优惠券 add_action( 'woocommerce_order_details_after_order_table', 'display_coupon_in_frontend_order_details', 10, 1 ); function display_coupon_in_frontend_order_details( $order ) { $coupon_code = $order->get_meta( '_customer_reward_coupon_code' ); if ( !empty( $coupon_code ) ) { $coupon_id = $order->get_meta( '_customer_reward_coupon_id' ); $expiry_date = get_post_meta( $coupon_id, 'expiry_date', true ); $amount = get_post_meta( $coupon_id, 'coupon_amount', true ); $discount_type = get_post_meta( $coupon_id, 'discount_type', true ); $discount = $amount . ( 'percent' == $discount_type ? '%' : get_woocommerce_currency() ); echo '<div style="margin: 2em 0; padding: 1em; border: 1px dashed #ccc; text-align:center;"> <h3>' . __( 'Your Exclusive Reward Coupon', 'woocommerce' ) . '</h3> <p>' . __( 'Thank you for your purchase! Here\'s your exclusive discount:', 'woocommerce' ) . '</p> <div style="font-size:1.5em; font-weight:bold; margin:1em 0;">' . $coupon_code . '</div> <p>' . __( 'Discount:', 'woocommerce' ) . ' ' . $discount . '</p> <p>' . __( 'Expires On:', 'woocommerce' ) . ' ' . $expiry_date . '</p> </div>'; } } // 我的账户订单列表添加优惠券列 add_filter( 'woocommerce_my_account_my_orders_columns', 'add_coupon_column_to_my_orders' ); function add_coupon_column_to_my_orders( $columns ) { $columns['reward_coupon'] = __( 'Reward Coupon', 'wo




