Php 在商业订单和电子邮件通知中显示产品品牌和名称

Php 在商业订单和电子邮件通知中显示产品品牌和名称,php,wordpress,woocommerce,orders,taxonomy-terms,Php,Wordpress,Woocommerce,Orders,Taxonomy Terms,在WooCommerce中,我启用了插件来显示产品品牌。我希望品牌在整个周期(单个产品页面、购物车、结账、迷你购物车、订单和电子邮件)中出现在产品名称之前 我能够在购物车和结帐页面中的产品名称之前显示相关品牌,使用“”回答代码稍微更改(使用pbw brandplugin自定义分类法): //在购物车和结账页面中显示产品品牌 添加过滤器('woocommerce\u cart\u item\u name','Customization\u cart\u item\u name',10,3); 自定

在WooCommerce中,我启用了插件来显示产品品牌。我希望品牌在整个周期(单个产品页面、购物车、结账、迷你购物车、订单和电子邮件)中出现在产品名称之前

我能够在购物车和结帐页面中的产品名称之前显示相关品牌,使用“”回答代码稍微更改(使用
pbw brand
plugin自定义分类法):

//在购物车和结账页面中显示产品品牌
添加过滤器('woocommerce\u cart\u item\u name','Customization\u cart\u item\u name',10,3);
自定义购物车项目名称($product\U name、$cart\U item、$cart\U item\U key)的函数{
$product=$cart_项目['data'];//产品
$product\U id=$cart\U item['product\U id'];//产品id
//循环浏览产品品牌名称
foreach(wp_get_post_terms($product_id,'pwb brand')作为$wp_term)
$brand_name[]=$wp_term->name;//在数组中设置品牌名称
$brand_names_str=inpolde(“,”,$brand_names);//在逗号分隔的字符串数组中设置品牌名称
$brand=$brand\U NAME\U str;
$product\U permalink=$product->get\U permalink($cart\U item);
if(is_cart()&&count($brand_name)>0)
返回sprintf(“”,esc_url($product_permalink),$brand,$product->get_name());
elseif(计数($brand_name)>0)
返回$brand.'.$product\U name;
否则返回$product\u名称;
}

但我不知道如何在订单和电子邮件通知中实现这一点。

我重新查看了您的问题代码,并添加了一些附加功能,以便在订单页面和电子邮件通知中显示产品品牌:

// Utility: Get the product brand term names (from the product ID)
function wc_get_product_brand( $product_id ) {
   return implode(', ', wp_get_post_terms($product_id, 'pwb-brand', ['fields' => 'names']));
}

// Display product brand in Cart and checkout pages
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'];          // The WC_Product Object
    $permalink = $product->get_permalink(); // The product permalink

    if( $brand = wc_get_product_brand( $cart_item['product_id'] ) ) {
        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;
}

// Display product brand in order pages and email notification
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
    $product = $item->get_product();        // The WC_Product Object
    $permalink = $product->get_permalink(); // The product permalink

    if( $brand = wc_get_product_brand( $item->get_product_id() ) ) {
        if ( is_wc_endpoint_url() )
            return sprintf('<a href="%s">%s %s</a>', esc_url($permalink), $brand, $product->get_name());
        else
            return  $brand . ' ' . $product_name;
    }
    return $product_name;
}
//实用程序:获取产品品牌术语名称(来自产品ID)
功能wc_get_product_brand($product_id){
返回内爆(“,”,wp_get_post_terms($product_id,'pwb brand',['fields'=>'names']);
}
//在购物车和结账页面中显示产品品牌
添加过滤器('woocommerce\u cart\u item\u name','Customization\u cart\u item\u name',10,3);
自定义购物车项目名称($product\U name、$cart\U item、$cart\U item\U key)的函数{
$product=$cart_项目['data'];//WC_产品对象
$permalink=$product->get_permalink();//产品permalink
如果($brand=wc\U get\U product\U brand($cart\U item['product\U id'])){
if(is_cart())
返回sprintf(“”,esc_url($permalink),$brand,$product->get_name());
其他的
返回$brand.'.$product\U name;
}
返回$product\u名称;
}
//在订单页面和电子邮件通知中显示产品品牌
添加过滤器('woocommerce\u order\u item\u name','Customized\u order\u item\u name',10,2);
自定义订单商品名称($product\U name,$item)的函数{
$product=$item->get_product();//WC_产品对象
$permalink=$product->get_permalink();//产品permalink
如果($brand=wc\U get\U product\U brand($item->get\U product\U id())){
如果(是_wc_endpoint_url())
返回sprintf(“”,esc_url($permalink),$brand,$product->get_name());
其他的
返回$brand.'.$product\U name;
}
返回$product\u名称;
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试,效果良好。

你真是个了不起的家伙!非常感谢你!是否也可以在“后端订单详细信息”和“产品”部分显示品牌?@FarrukhKarimov由于订单项是动态可编辑数据,因此应采用不同的方式(不确定是否可以在产品名称中显示品牌)。所以你应该问一个新问题。
// Utility: Get the product brand term names (from the product ID)
function wc_get_product_brand( $product_id ) {
   return implode(', ', wp_get_post_terms($product_id, 'pwb-brand', ['fields' => 'names']));
}

// Display product brand in Cart and checkout pages
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'];          // The WC_Product Object
    $permalink = $product->get_permalink(); // The product permalink

    if( $brand = wc_get_product_brand( $cart_item['product_id'] ) ) {
        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;
}

// Display product brand in order pages and email notification
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
    $product = $item->get_product();        // The WC_Product Object
    $permalink = $product->get_permalink(); // The product permalink

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