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

如何将pwb-brand自定义分类法的术语元数据添加到WooCommerce购物车等场景

实现方案

核心思路

你之前的代码调用wp_get_post_terms时指定了fields => 'names'参数,仅返回分类术语名称,自然无法拿到术语ID调取自定义元数据。我们可以先封装一个公共工具函数统一获取品牌+ETA信息,再嵌入到现有三个展示场景的钩子中,避免代码重复。

完整实现代码

把以下代码替换你原来主题functions.php中的相关代码即可:

// 公共工具函数:统一获取商品关联的品牌+ETA信息
function get_pwb_brand_with_eta( $product_id ) {
    $output = '';
    $brands = wp_get_object_terms( $product_id, 'pwb-brand' );
    if ( empty( $brands ) || is_wp_error( $brands ) ) return $output;

    foreach ( $brands as $brand ) {
        $eta_start = get_term_meta( $brand->term_id, 'pwb_brand_eta_start', true );
        $eta_end = get_term_meta( $brand->term_id, 'pwb_brand_eta_end', true );
        // 拼接品牌名称
        $output .= '<div>' . __( 'Brand', 'perfect-woocommerce-brands' ) . ': ' . esc_html( $brand->name ) . '</div>';
        // 已设置ETA则拼接交货周期,规则和商品详情页保持一致
        if ( $eta_start && $eta_end ) {
            $eta_date_start = date( 'm/d', strtotime( '+' . $eta_start . ' weeks' ) );
            $eta_date_end = date( 'm/d', strtotime( '+' . $eta_end . ' weeks' ) );
            $output .= '<div class="pwb-eta"><small><strong>Availability: </strong> Estimated ' . esc_html( $eta_start ) . '-' . esc_html( $eta_end ) . ' weeks (' . esc_html( $eta_date_start ) . '-' . esc_html( $eta_date_end ) . ')</small></div>';
        }
    }
    return $output;
}

// 购物车、结算页展示品牌+ETA
add_filter( 'woocommerce_cart_item_name', function( $link_text, $cart_item, $cart_item_key ) {
    $_product = $cart_item['data'];
    $product_id = $_product->get_id();
    // 调用工具函数获取品牌+ETA信息
    $brand_eta_info = get_pwb_brand_with_eta( $product_id );
    $link_text = $brand_eta_info;

    $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
    if ( ! $product_permalink ) {
        $link_text .= $_product->get_name();
    } else {
        $link_text .= sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() );
    }
    return $link_text;
}, 10, 3 );

// 前端订单页、邮件通知展示品牌+ETA
add_action( 'woocommerce_order_item_meta_end', 'display_brand_eta_in_order_and_email', 10, 4 );
function display_brand_eta_in_order_and_email( $item_id, $item, $order, $bool ) {
    $product_id = $item->get_product_id();
    echo get_pwb_brand_with_eta( $product_id );
}

// 后台订单编辑页展示品牌+ETA
add_action( 'woocommerce_after_order_itemmeta', 'display_brand_eta_in_admin_order', 15, 3 );
function display_brand_eta_in_admin_order( $item_id, $item, $product ) {
    if ( $item->is_type( 'line_item' ) ) {
        $product_id = $item->get_product_id();
        echo get_pwb_brand_with_eta( $product_id );
    }
}

注意事项

  • 所有输出做了转义处理,符合WordPress安全开发规范
  • 兼容无关联品牌、未设置ETA的场景,不会抛出PHP错误
  • ETA展示规则和你现有商品详情页的逻辑完全对齐

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 16:30:04