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

如何在WooCommerce订单完成通知中添加商品短描述至标题后?

WooCommerce订单完成邮件添加商品短描述的正确代码

你遇到的「Cannot redeclare function」错误,是因为原代码使用的函数名已被其他主题/插件占用,导致函数重复声明。以下是修正后的代码,换用了唯一的函数名,且仅在订单完成邮件中生效:

add_filter( 'woocommerce_order_item_name', 'custom_add_product_short_desc_to_completed_email', 10, 3 );
function custom_add_product_short_desc_to_completed_email( $item_name, $item, $is_visible ) {
    // 仅在订单完成邮件中生效
    if ( ! is_wc_endpoint_url() && isset( $GLOBALS['email'] ) && $GLOBALS['email']->id === 'customer_completed_order' ) {
        $product = $item->get_product();
        // 检查商品是否存在且有短描述
        if ( $product && $product->get_short_description() ) {
            $item_name .= '<br><small>' . wp_kses_post( $product->get_short_description() ) . '</small>';
        }
    }
    return $item_name;
}

代码说明:

  • 使用woocommerce_order_item_name过滤器精准修改商品标题的显示内容
  • 通过判断邮件ID确保仅在「订单完成」邮件中添加短描述,不影响其他场景
  • 用<br><small>标签做样式区分,让短描述显示在标题下方且字体更小,符合邮件排版逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 19:29:57