(called "BANISH")We are using Woocommerce Brands on the site but need to show brand next to the name site wide for both customers and admins responsible for picking stock. I have managed to insert the brand name using hooks on single product pages, cart, checkout and email invoices. I need to add the brand for each product to the admin order screens in the backend.
I have successfully added the brand into the order page using the following code
add_action('woocommerce_before_order_itemmeta','woocommerce_before_order_itemmeta',10,3);
function woocommerce_before_order_itemmeta($item_id, $item, $product){
echo '<p>'.get_the_term_list($product->id, 'product_brand').'</p>';
}
However, this puts the brand below the meta - how can I alter this to make it appear above (or even better before the product name on each item). See linked screenshot below where brand (in this instance called "BANISH") appears underneath item meta.
UPDATE - I have already used the 'woocommerce_order_item_name' hook as well as the 'woocommerce_cart_item_name' and 'woocommerce_cart_item_name' hooks but none of these seem to effect the edit order pages. Here are the set of hooks currently deployed:
// 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, 'product_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;
}
// Display brand name on single product pages
/*
* Increase `1` on line 7 to move position of brand name
* */
add_action( 'woocommerce_single_product_summary','wc_brands_add_brand_name', 4 );
function wc_brands_add_brand_name() {
global $product;
$brands = implode(', ', wp_get_post_terms($product->get_id(), 'product_brand', ['fields' => 'names']));
echo "<h3>" . $brands . "</h3>";
}
//Display product brand in admin edit orders screens
add_action('woocommerce_before_order_itemmeta','woocommerce_before_order_itemmeta',10,3);
function woocommerce_before_order_itemmeta($item_id, $item, $product){
echo '<p>'.get_the_term_list($product->id, 'product_brand').'</p>';
}
First, I have updated the thread where you picked some of your current code.
Now woocommerce_order_item_name
is not the right hook to make changes to the order item name in admin single orders. The right hook is woocommerce_order_item_get_name
, taking care to target admin pages and "line_item" order item type.
The following code is compatible with High Performance Order Storage (HPOS):
// Utility function: Get the product brand term name(s) (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 product brand name(s) in admin edit order pages
add_filter( 'woocommerce_order_item_get_name', 'display_admin_order_item_product_brands', 10, 2 );
function display_admin_order_item_product_brands( $item_name, $item ) {
if ( ! ( is_admin() && $item->is_type('line_item') ) ) {
return $item_name;
}
global $pagenow, $typenow;
$page = isset($_GET['page']) ? $_GET['page'] : '';
$action = isset($_GET['action']) ? $_GET['action'] : '';
if ( $action === 'edit' && ( ( $pagenow === 'post.php' && $typenow === 'shop_order' )
|| ( $pagenow === 'admin.php' && $page === 'wc-orders' ) ) ) {
if ( $brands = wc_get_product_brands( $item->get_product_id() ) ) {
return $brands . ' <br>' . $item_name;
}
}
return $item_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.