在Woocommerce订单和电子邮件通知中显示产品品牌和名称 [英] Display Product Brand and Name in Woocommerce Orders and email notifications

查看:102
本文介绍了在Woocommerce订单和电子邮件通知中显示产品品牌和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In WooCommerce I have enabled Perfect Brands Woocommerce plugin to display product Brands. I would like the Brand to appear before the product name throughout the whole cycle (single product page, cart, checkout, mini cart, order and emails).

我可以使用"答案代码已稍作更改(使用>插件自定义分类法) :

// 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 product
    $product_id =  $cart_item['product_id']; // The product id

    // Loop through the product brand names
    foreach( wp_get_post_terms( $product_id, 'pwb-brand' ) as $wp_term )
        $brand_names[] = $wp_term->name; // Set the brand names in an array

    $brand_names_str = implode( ', ', $brand_names); // Set the brand names in a comma separated string array

    $brand = $brand_names_str;
    $product_permalink = $product->get_permalink( $cart_item );

    if ( is_cart() && count( $brand_names ) > 0 )
        return sprintf( '<a href="%s">%s %s</a>', esc_url( $product_permalink ), $brand, $product->get_name()  );
    elseif ( count( $brand_names ) > 0 )
        return  $brand . ' ' . $product_name;
    else return $product_name;
}

但是我不知道如何针对订单"和电子邮件"通知实施该操作.

But I don't know how to implement that for the Order and Email notifications.

推荐答案

我已经重新访问了您的问题代码,并添加了一些其他功能以在订购"页面和电子邮件通知上显示产品品牌:

I have revisited your question code and added some additional functions to display the product brand on Order pages and on email notifications:

// 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;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce订单和电子邮件通知中显示产品品牌和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆