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

WooCommerce订单及邮件通知自定义商品描述问题求助

解决WooCommerce仅在订单邮件/支付收据中显示自定义商品描述的问题

问题背景

需要修改商品在**订单邮件、支付收据(PayPal/信用卡)**等售后场景的显示描述,已通过自定义字段保存order_description,但原代码会全局修改描述,要求不改动商品原描述/短描述,仅针对指定场景替换。

原尝试代码及问题

add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_15127954', 10, 2 );
function wc_checkout_description_so_15127954( $other_data, $cart_item ) {
    $post_data = get_post( $cart_item['product_id'] );
    $custom = get_post_meta( get_the_ID(), 'order_description', true );
    $other_data[] = array( 'name' =>  $post_data->$custom );
    return $other_data;
}

原代码问题:

  • 全局调用woocommerce_get_item_data钩子,会在购物车、结账页等所有场景添加自定义描述,不符合需求;
  • 代码逻辑错误:get_the_ID()在该钩子中不一定指向商品ID,$post_data->$custom写法无效。

已实现的自定义字段代码(中文修正版)

以下是修正后的商品后台自定义字段代码,用于输入订单专用描述:

// 在商品常规选项卡添加自定义订单描述字段
add_action( 'woocommerce_product_options_general_product_data', 'custom_description_custom_field' );
function custom_description_custom_field() {
    $args = array(
        'id'            => 'order_description',
        'label'         => __( '订单专用描述', 'text_domain' ),
        'class'         => 'product-description-field',
        'desc_tip'      => true,
        'description'   => __( '该描述将显示在PayPal、信用卡收据、订单邮件等售后场景', 'text_domain' ),
    );
    woocommerce_wp_text_input( $args );
}

// 保存商品自定义订单描述字段
add_action( 'woocommerce_process_product_meta', 'save_custom_description_custom_field' );
function save_custom_description_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $custom_desc = isset( $_POST['order_description'] ) ? sanitize_text_field( $_POST['order_description'] ) : '';
    $product->update_meta_data( 'order_description', $custom_desc );
    $product->save();
}

注:修正了原代码中文本域的错误写法,优化了变量名和描述文本。


解决方案:仅在指定场景替换商品描述

针对不同场景使用对应的WooCommerce钩子,确保仅在订单邮件、支付收据等售后场景生效,不影响前端展示:

1. 订单邮件、后台订单详情页的描述替换

使用woocommerce_order_item_name钩子,替换订单中商品名称下方的描述:

add_filter( 'woocommerce_order_item_name', 'replace_order_item_description', 10, 3 );
function replace_order_item_description( $item_name, $item, $is_visible ) {
    // 仅在后台订单详情、订单邮件中生效,排除前端购物车、结账页、商品页
    if ( ! is_wc_endpoint_url( 'view-order' ) && ! wc_get_page_id( 'shop' ) && ! is_cart() && ! is_checkout() ) {
        $product = $item->get_product();
        $custom_desc = $product ? $product->get_meta( 'order_description' ) : '';
        if ( ! empty( $custom_desc ) ) {
            $item_name .= '<br><small>' . esc_html( $custom_desc ) . '</small>';
        }
    }
    return $item_name;
}

2. PayPal/信用卡支付收据的描述替换

针对主流支付网关分别处理,确保支付收据显示自定义描述:

// PayPal支付时替换商品描述
add_filter( 'woocommerce_paypal_args', 'replace_paypal_item_description' );
function replace_paypal_item_description( $args ) {
    $order = wc_get_order( $args['invoice'] );
    if ( $order ) {
        foreach ( $order->get_items() as $item_id => $item ) {
            $product = $item->get_product();
            $custom_desc = $product ? $product->get_meta( 'order_description' ) : '';
            if ( ! empty( $custom_desc ) ) {
                $args['item_name_' . ( $item_id + 1 )] = $custom_desc;
            }
        }
    }
    return $args;
}

// Stripe信用卡支付时替换商品描述(适配官方Stripe插件)
add_filter( 'woocommerce_stripe_payment_metadata', 'replace_stripe_item_description', 10, 2 );
function replace_stripe_item_description( $metadata, $order ) {
    foreach ( $order->get_items() as $item_id => $item ) {
        $product = $item->get_product();
        $custom_desc = $product ? $product->get_meta( 'order_description' ) : '';
        if ( ! empty( $custom_desc ) ) {
            $metadata[ '商品 ' . ( $item_id + 1 ) ] = $custom_desc;
        }
    }
    return $metadata;
}

代码说明

  • 所有钩子均做了场景判断,仅在订单邮件、后台订单、支付网关请求时生效,不会影响前端购物车、商品展示页;
  • 仅当商品设置了"订单专用描述"时才替换,未设置则保留原描述;
  • 针对不同支付网关分别处理,确保PayPal和信用卡收据都能显示自定义描述。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 16:57:39