自定义Pending状态新订单邮件:商品及小计缺失问题求助
Hey there, let's tackle your two main problems step by step—fixing the broken content in your existing pending order email, and setting up a dedicated notification that only triggers for "Pending" status orders.
1. Fixing Missing Products & Incorrect Subtotals (Total Amount Shows Correctly)
Since you've ruled out your custom payment module, let's focus on the email template logic and order data integrity:
Verify your email template's product loop code
Double-check how you're pulling order items in the template. For example, if you're using a PHP-based e-commerce framework, ensure you're accessing the correct order item properties instead of relying on raw array access:// Example loop for order items (adjust to match your system) foreach ($order->get_line_items() as $item) { $product_name = $item->get_product_name(); $quantity = $item->get_quantity(); $item_subtotal = $item->get_calculated_subtotal(); // Price × quantity before adjustments // Render product line in email here }Mixing up fields like
subtotalvstotal(which might include discounts/taxes) is a common cause of incorrect subtotals.Validate order data before email triggers
Add a debug log right before sending the email to confirm the order's item data is complete. This helps you rule out incomplete order data as the root cause:// Log order item details to your server's error log error_log("Pending Order #{$order->id} Items: " . print_r($order->line_items, true));If the log shows missing product names or wrong subtotals, check your order creation logic—ensure all item data is properly saved to the database before the email fires.
Check the email trigger timing
Make sure the email is triggered after the order and its items are fully saved. If you're using a hook that fires too early (e.g., before order items are inserted into the database), the email will pull incomplete data. Switch to a hook that triggers when the order status is updated to "Pending" instead.Confirm subtotal calculation logic
If subtotals are wrong but the total is correct, you might be ignoring line-item discounts or tax adjustments in your template. Ensure your subtotal uses the raw item price × quantity, or matches how your system calculates line-item subtotals (pre-discount/pre-tax vs post-adjustment).
2. Setting Up a Dedicated Pending Order Notification
To create a notification that only triggers for "Pending" status orders, follow these steps:
Create custom email templates
Duplicate your existing order email template, rename them (e.g.,pending-order-admin.phpandpending-order-customer.php), and adjust the content to explicitly highlight the pending payment status. Customize subject lines too—likeNew Pending Order #{{order_id}} - Awaiting Paymentfor admins, andYour Pending Order #{{order_id}} - Complete Payment Nowfor customers.Add a status-specific trigger
Hook into your system's order status change event to send the new emails only when the order is set to "Pending". Here's a PHP example tailored to most custom e-commerce systems:// Trigger when order status is updated to Pending add_action('order_status_updated_to_pending', 'send_pending_order_notifications', 10, 2); function send_pending_order_notifications($order_id, $order) { // Send admin notification $admin_email = get_config('admin_email'); // Replace with your admin email retrieval logic $admin_subject = "New Pending Order #{$order_id}"; $admin_content = load_email_template('pending-order-admin.php', ['order' => $order]); mail($admin_email, $admin_subject, $admin_content, 'Content-Type: text/html'); // Send customer notification $customer_email = $order->billing_email; $customer_subject = "Your Pending Order #{$order_id} - Please Complete Payment"; $customer_content = load_email_template('pending-order-customer.php', ['order' => $order]); mail($customer_email, $customer_subject, $customer_content, 'Content-Type: text/html'); }Adjust the hook name and template loading function to match your system's architecture.
Prevent duplicate notifications
If your existing new order email also triggers for Pending orders, add a condition to disable it for this status:// In your existing new order email trigger function if ($order->status !== 'pending') { // Send original new order email }Test thoroughly
Create a test order, set its status to "Pending", and verify both admin and customer receive the correct emails with complete product details and accurate amounts.
内容的提问来源于stack exchange,提问作者PHP Developer

