You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

WooCommerce后台编辑订单页面将产品品牌显示至产品名称上方的实现方法

WooCommerce后台编辑订单页面将产品品牌显示至产品名称上方的实现方法

我太懂你这种卡壳的感觉了——前台、购物车、邮件里的品牌显示都搞定了,唯独后台编辑订单页的品牌位置怎么都调不对,之前用woocommerce_before_order_itemmeta把品牌放到了meta下面,想挪到产品名称前面却没头绪,而且试了woocommerce_order_item_name钩子还没生效,确实让人头疼。

问题根源

你之前用woocommerce_order_item_name没效果,大概率是两个原因:

  1. 钩子优先级不够,被其他同钩子的函数覆盖了;
  2. 没有专门针对后台编辑页的场景做判断,WooCommerce在后台渲染订单项时,is_wc_endpoint_url()会返回false,虽然你之前的else分支处理了,但逻辑不够精准。

解决方案:调整钩子逻辑+优先级

我们可以复用你已经写好的品牌获取工具函数,然后优化woocommerce_order_item_name的过滤逻辑,专门针对后台编辑页做处理,同时提高钩子优先级确保生效:

// Utility: Get the product brand term names (from the product ID) - 优化版
function wc_get_product_brand( $product_id ) {
    $terms = wp_get_post_terms( $product_id, 'product_brand', ['fields' => 'names'] );
    // 处理无品牌的情况,避免输出空内容
    return ! empty( $terms ) ? implode(', ', $terms) : '';
}

// 1. 购物车/结账页面显示品牌+产品名称
add_filter( 'woocommerce_cart_item_name','customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
    $product = $cart_item['data'];
    $permalink = $product->get_permalink();
    $brand = wc_get_product_brand( $cart_item['product_id'] );

    if( $brand ) {
        if ( is_cart() ) {
            return sprintf('<a href="%s">%s %s</a>', esc_url($permalink), $brand, $product->get_name());
        } else {
            return $brand . ' ' . $product_name;
        }
    }
    return $product_name;
}

// 2. 订单页(前台我的账户+后台编辑页)显示品牌+产品名称 - 优化版
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 20, 2 );
function customizing_order_item_name( $product_name, $item ) {
    // 只处理实体产品的订单项,跳过运费、优惠券等类型
    if ( ! $item->is_type('line_item') ) return $product_name;

    $product_id = $item->get_product_id();
    $product = $item->get_product();

    // 产品不存在时直接返回原名称,避免报错
    if ( ! $product ) return $product_name;

    $brand = wc_get_product_brand( $product_id );
    if( $brand ) {
        if ( is_wc_endpoint_url() ) {
            // 前台我的账户订单详情页,保留产品链接
            $permalink = $product->get_permalink();
            return sprintf('<a href="%s">%s %s</a>', esc_url($permalink), $brand, $product->get_name());
        } elseif ( is_admin() ) {
            // 后台编辑订单页,直接显示品牌+名称
            return $brand . ' ' . $product_name;
        } else {
            // 其他前台场景
            return $brand . ' ' . $product_name;
        }
    }
    return $product_name;
}

// 3. 单个产品页面显示品牌
add_action( 'woocommerce_single_product_summary','wc_brands_add_brand_name', 4 );
function wc_brands_add_brand_name() {
    global $product;
    $brands = wc_get_product_brand( $product->get_id() );
    if ( $brands ) {
        echo "<h3>{$brands}</h3>";
    }
}

关键调整点

  1. 提高钩子优先级:把woocommerce_order_item_name的优先级从10调到20,确保我们的函数能覆盖其他同钩子的逻辑;
  2. 精准场景判断:新增is_admin()分支专门处理后台编辑页,直接输出品牌+产品名称;
  3. 容错处理:在工具函数和订单项函数里都加了空值判断,避免产品不存在或无品牌时报错;
  4. 移除冗余代码:你之前用的woocommerce_before_order_itemmeta动作可以删掉了,现在通过woocommerce_order_item_name就能直接把品牌放到产品名称前面,完全符合你的需求。

效果验证

用这个代码替换你之前的对应函数后,后台编辑订单页的每个产品项都会显示成「品牌名称 产品名称」的格式,比如你截图里的「BANISH 产品名」,完美解决位置问题。

内容来源于stack exchange

火山引擎 最新活动