You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce订单邮件中添加旅游产品特色信息?

解决WooCommerce旅游订单邮件添加自定义产品字段的问题

问题分析

你之前的代码失败核心原因是:没有针对订单中的具体旅游产品获取元数据,而是直接调用全局方法,导致无法定位到对应产品的自定义字段。订单中可能包含多个产品,必须遍历每个订单商品,指定产品ID来获取主题定义的旅游字段。

方案一:使用WooCommerce邮件钩子(推荐,无需修改模板)

将以下代码添加到子主题的functions.php文件中:

add_action('woocommerce_email_order_meta', 'add_tour_custom_fields_to_email', 10, 3);
function add_tour_custom_fields_to_email($order_id, $sent_to_admin, $order) {
    // 仅向客户邮件显示,跳过管理员邮件
    if ($sent_to_admin) return;

    foreach ($order->get_items() as $item) {
        $product = $item->get_product();
        // 确认当前商品是旅游产品(根据主题实际产品类型调整,比如'tour')
        if ($product->get_type() !== 'tour') continue;

        $product_id = $product->get_id();

        // 1. 输出旅游亮点
        $highlight_title = Egns\Helper\Egns_Helper::egns_tours_value('tours_highlights_title', $product_id);
        $highlight_list = Egns\Helper\Egns_Helper::egns_tours_value('tours_highlights_title_list', $product_id);
        
        if ($highlight_list) {
            echo '<h3>' . esc_html($highlight_title ?: 'Highlights of the Tour') . '</h3>';
            echo '<ul>';
            foreach (explode("\n", $highlight_list) as $item) {
                $trimmed_item = trim($item);
                if (!empty($trimmed_item)) {
                    echo '<li>' . esc_html($trimmed_item) . '</li>';
                }
            }
            echo '</ul>';
        }

        // 2. 输出包含项目(替换为主题实际字段ID)
        $includes_title = Egns\Helper\Egns_Helper::egns_tours_value('tours_includes_title', $product_id);
        $includes_list = Egns\Helper\Egns_Helper::egns_tours_value('tours_includes_list', $product_id);
        
        if ($includes_list) {
            echo '<h3>' . esc_html($includes_title ?: 'Included Items') . '</h3>';
            echo '<ul>';
            foreach (explode("\n", $includes_list) as $item) {
                $trimmed_item = trim($item);
                if (!empty($trimmed_item)) {
                    echo '<li>' . esc_html($trimmed_item) . '</li>';
                }
            }
            echo '</ul>';
        }

        // 3. 输出排除项目(替换为主题实际字段ID)
        $excludes_title = Egns\Helper\Egns_Helper::egns_tours_value('tours_excludes_title', $product_id);
        $excludes_list = Egns\Helper\Egns_Helper::egns_tours_value('tours_excludes_list', $product_id);
        
        if ($excludes_list) {
            echo '<h3>' . esc_html($excludes_title ?: 'Excluded Items') . '</h3>';
            echo '<ul>';
            foreach (explode("\n", $excludes_list) as $item) {
                $trimmed_item = trim($item);
                if (!empty($trimmed_item)) {
                    echo '<li>' . esc_html($trimmed_item) . '</li>';
                }
            }
            echo '</ul>';
        }

        // 4. 输出行程(替换为主题实际字段ID,若为富文本则用wpautop处理)
        $itinerary_title = Egns\Helper\Egns_Helper::egns_tours_value('tours_itinerary_title', $product_id);
        $itinerary_content = Egns\Helper\Egns_Helper::egns_tours_value('tours_itinerary_content', $product_id);
        
        if ($itinerary_content) {
            echo '<h3>' . esc_html($itinerary_title ?: 'Tour Itinerary') . '</h3>';
            echo wpautop(esc_html($itinerary_content));
        }
    }
}

关键说明

  • 调用egns_tours_value时必须传入第二个参数$product_id——主题Helper方法默认获取当前全局文章的元数据,而邮件钩子环境中全局文章不是目标旅游产品。
  • 根据主题实际的产品类型调整$product->get_type()的判断值(比如有些主题用tour或travel)。
  • 替换代码中的字段ID为主题实际定义的包含/排除项目、行程的ID(参考tours.php中的字段配置)。

方案二:直接修改邮件模板

如果需要更灵活的布局控制,可以复制主题(或WooCommerce默认)的customer-processing-email.php到子主题的woocommerce/emails/目录下,然后在订单详情区域之后添加以下代码:

<?php
// 遍历订单中的旅游产品
foreach ($order->get_items() as $item) {
    $product = $item->get_product();
    if ($product->get_type() !== 'tour') continue;

    $product_id = $product->get_id();
?>

<h2><?php esc_html_e('Tour Details', 'triprex-core'); ?></h2>

<?php
// 旅游亮点
$highlight_title = Egns\Helper\Egns_Helper::egns_tours_value('tours_highlights_title', $product_id);
$highlight_list = Egns\Helper\Egns_Helper::egns_tours_value('tours_highlights_title_list', $product_id);
if ($highlight_list) :
?>
    <h3><?php echo esc_html($highlight_title ?: 'Highlights of the Tour'); ?></h3>
    <ul>
        <?php foreach (explode("\n", $highlight_list) as $highlight) :
            $highlight = trim($highlight);
            if (!empty($highlight)) : ?>
                <li><?php echo esc_html($highlight); ?></li>
        <?php endif; endforeach; ?>
    </ul>
<?php endif; ?>

<?php
// 包含项目(替换字段ID)
$includes_list = Egns\Helper\Egns_Helper::egns_tours_value('tours_includes_list', $product_id);
if ($includes_list) :
?>
    <h3><?php esc_html_e('Included Items', 'triprex-core'); ?></h3>
    <ul>
        <?php foreach (explode("\n", $includes_list) as $item) :
            $item = trim($item);
            if (!empty($item)) : ?>
                <li><?php echo esc_html($item); ?></li>
        <?php endif; endforeach; ?>
    </ul>
<?php endif; ?>

<?php
// 行程(富文本示例)
$itinerary_content = Egns\Helper\Egns_Helper::egns_tours_value('tours_itinerary_content', $product_id);
if ($itinerary_content) :
?>
    <h3><?php esc_html_e('Tour Itinerary', 'triprex-core'); ?></h3>
    <?php echo wpautop(esc_html($itinerary_content)); ?>
<?php endif; ?>

<?php } ?>

注意事项

  • 子主题模板会覆盖主题或WooCommerce默认模板,避免主题更新丢失修改。
  • 保持模板中的HTML结构与原邮件一致,避免破坏邮件样式。

内容的提问来源于stack exchange,提问作者editix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 14:53:10