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

求助:实现WooCommerce自定义字段在FooEvents票务中按需展示

解决方案

核心思路

通过FooEvents票务关联的订单对象,遍历订单内所有商品,检查是否存在ID为90的ABC商品,根据检查结果决定是否显示指定内容。

代码实现

方式1:显示“ABC”文字

<?php
// 获取当前票务关联的订单对象
$order = wc_get_order($ticket['order_id']);
$has_abc_product = false;

// 遍历订单商品,检查是否存在ID为90的商品
foreach ($order->get_items() as $item) {
    if ($item->get_product_id() == 90) {
        $has_abc_product = true;
        break;
    }
}

// 存在目标商品则显示文字
if ($has_abc_product) : ?>
    ABC
<?php endif; ?>

方式2:显示PNG图片

<?php
// 获取当前票务关联的订单对象
$order = wc_get_order($ticket['order_id']);
$has_abc_product = false;

// 遍历订单商品,检查是否存在ID为90的商品
foreach ($order->get_items() as $item) {
    if ($item->get_product_id() == 90) {
        $has_abc_product = true;
        break;
    }
}

// 存在目标商品则显示图片,替换为你的实际图片路径
if ($has_abc_product) : ?>
    <img src="你的ABC图片URL路径.png" alt="ABC" />
<?php endif; ?>

注意事项

  • 若ABC商品是变体,需将代码中的get_product_id()替换为get_variation_id(),或同时检查两个ID以覆盖所有情况
  • 图片路径请替换为WordPress媒体库内对应的PNG图片链接
  • 代码需插入到FooEvents票务模板的对应位置,和你提供的公司名称示例代码位置保持一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 14:34:57