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).
I am able to display the related brand before the product name in cart and checkout pages, using "Adding Woocommerce Brands names to cart item product names" answer code lightly changed (using pbw-brand
plugin custom taxonomy):
// 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.
Updated: code optimization
I have revisited your question code and added some additional functions to display the product brand on Order pages and on email notifications:
// Utility function: Get the product brand term names (from the product ID)
function wc_get_product_brands( $product_id ) {
$terms = get_the_terms($product_id, 'product_brand'); // Get Cached product brand terms
if ( $terms && ! is_wp_error( $terms ) ) {
return implode( ', ', wp_list_pluck($terms, 'name') );
}
}
// Display brand name(s) on single product pages
add_action( 'woocommerce_single_product_summary','display_single_product_brands', 4 );
function display_single_product_brands() {
global $product;
if ( $brands = wc_get_product_brands( $product->get_id() ) ) {
printf( '<h3 class="brand">%s</h3>', $brands );
}
}
// Display product brand name(s) in Cart and checkout pages
add_filter( 'woocommerce_cart_item_name','display_cart_item_product_brands', 10, 3 );
function display_cart_item_product_brands( $product_name, $cart_item, $cart_item_key ) {
if( $brands = wc_get_product_brands( $cart_item['product_id'] ) ) {
if ( is_cart() ) {
return sprintf('<a href="%s">%s %s</a>', esc_url($cart_item['data']->get_permalink()), $brands, $product_name);
} else {
return $brands . ' ' . $product_name;
}
}
return $product_name;
}
// Display product brand name(s) in order pages and email notification
add_filter( 'woocommerce_order_item_name', 'display_order_item_product_brands', 10, 2 );
function display_order_item_product_brands( $product_name, $item ) {
if( $brands = wc_get_product_brands( $item->get_product_id() ) ) {
if ( is_wc_endpoint_url() ) {
$product = $item->get_product(); // The WC_Product Object
return sprintf('<a href="%s">%s %s</a>', esc_url($product->get_permalink()), $brands, $product_name);
} else {
return $brands . ' ' . $product_name;
}
}
return $product_name;
}
Code goes in functions.php file of your child theme (or in a plugin file). Tested and works.
Cached post term queries
Is better to use WordPress get_the_terms()
, as the query is cached (for performance), rather than WordPress wp_get_post_terms()
, which is not.