WooCommerce新订单钩子函数中商品详情邮件不显示求助
WooCommerce订单邮件商品详情不显示问题修复
需求
在WooCommerce创建新订单后,为订单内每个产品向其ACF自定义字段(email_gestore)存储的邮箱发送邮件,邮件需包含订单摘要中的商品详情。
原尝试代码
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 2 ); function create_invoice_for_wc_order( $order_id, $order ) { $oggetto = ''; $corpo = ''; $emailDaChi = $order->get_billing_email(); foreach ( $order->get_items() as $item_id => $item ) { $emailAChi = get_field('email_gestore', $product_id); //email del gestore if ($emailAChi != '' && $emailAChi != null) { $product_id = $item->get_product_id(); $variation_id = $item->get_variation_id(); $product = $item->get_product(); $product_name = $item->get_name(); $quantity = $item->get_quantity(); $subtotal = $item->get_subtotal(); $total = $item->get_total(); $tax = $item->get_subtotal_tax(); $tax_class = $item->get_tax_class(); $tax_status = $item->get_tax_status(); $allmeta = $item->get_meta_data(); $somemeta = $item->get_meta( '_whatever', true ); $item_type = $item->get_type(); $dettaglio = wc_display_item_meta($item, array( 'echo' => false, )); $corpo = ' <div> <h3> Riepilogo NUOVA PRENOTAZIONE: </h3> <p> DETTAGLI ORDINE: <br>' .$product_name.' <br> QUANTITA: '.$quantity.' <br> SUBTOTALE: '.$subtotal.' <br> TOTALE: '.$total.' <br> DETTAGLI: '.$dettaglio.' <br> </p> </div>'; $headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: '.$emailDaChi, ); $mail = wp_mail( $emailAChi, $oggetto, $corpo, $headers); } } }
问题
发送的邮件中「DETTAGLI:」后无内容,但直接echo邮件内容时商品详情能正常显示。
解决方案
问题根源
- 变量顺序错误:调用
get_field('email_gestore', $product_id)时,$product_id尚未定义,导致无法正确获取管理员邮箱,后续邮件逻辑可能未触发或数据异常。 - 钩子优先级过早:
woocommerce_new_order钩子优先级设为1时,订单的商品元数据可能尚未完全加载,导致wc_display_item_meta无法获取到内容。 - 邮件主题为空:
$oggetto未赋值,部分邮件服务商可能过滤空主题的邮件,导致内容显示异常。 - ACF字段调用对象错误:若产品是变种,需优先使用
$variation_id调用ACF字段(如果字段绑定到变种)。
修正后的代码
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 20, 2 ); function create_invoice_for_wc_order( $order_id, $order ) { // 设置邮件主题 $oggetto = 'Nuova prenotazione - Ordine #' . $order_id; $emailDaChi = sprintf( '%s %s <%s>', $order->get_billing_first_name(), $order->get_billing_last_name(), $order->get_billing_email() ); foreach ( $order->get_items( 'line_item' ) as $item_id => $item ) { $product_id = $item->get_product_id(); $variation_id = $item->get_variation_id(); // 优先获取变种的ACF字段,若无则使用主产品 $emailAChi = $variation_id ? get_field('email_gestore', $variation_id) : get_field('email_gestore', $product_id); if ( empty($emailAChi) ) continue; $product = $item->get_product(); $product_name = $item->get_name(); $quantity = $item->get_quantity(); $subtotal = wc_price( $item->get_subtotal() ); $total = wc_price( $item->get_total() ); // 确保获取商品元数据,使用wc_get_formatted_item_meta更可靠 $dettaglio = wc_get_formatted_item_meta( $item, array( 'before' => '<ul>', 'after' => '</ul>', 'separator' => '</li><li>', ) ); // 处理无元数据的情况 $dettaglio = empty($dettaglio) ? 'Nessun dettaglio aggiuntivo' : $dettaglio; $corpo = ' <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;"> <h3 style="color: #2c3e50;">Riepilogo NUOVA PRENOTAZIONE:</h3> <p style="font-size: 16px; line-height: 1.6;"> <strong>DETTAGLI ORDINE:</strong><br> Prodotto: ' . $product_name . '<br> Quantità: ' . $quantity . '<br> Subtotale: ' . $subtotal . '<br> Totale: ' . $total . '<br> <strong>Dettagli aggiuntivi:</strong> ' . $dettaglio . '<br> </p> </div>'; $headers = array( 'Content-Type: text/html; charset=UTF-8', 'From: ' . $emailDaChi, ); wp_mail( $emailAChi, $oggetto, $corpo, $headers ); } }
关键修改说明
- 调整钩子优先级:将优先级改为20,确保订单及其商品数据完全加载完成后再执行邮件逻辑。
- 修正变量顺序:先获取
product_id和variation_id,再调用ACF字段,确保邮箱地址正确获取。 - 变种产品兼容:优先使用变种ID调用ACF字段,适配绑定到变种的自定义字段场景。
- 元数据获取优化:改用
wc_get_formatted_item_meta替代wc_display_item_meta,该函数更适合在非前端场景获取格式化的元数据,并处理了无元数据的默认显示。 - 完善邮件内容:添加邮件主题、格式化邮件样式,避免空主题被过滤,同时提升邮件可读性。
- 简化逻辑:用
empty()判断邮箱是否存在,代码更简洁;仅遍历line_item类型的商品,排除运费、优惠券等非产品项。
内容的提问来源于stack exchange,提问作者elle D
相关产品推荐
相关产品推荐

