Skip to content

Instantly share code, notes, and snippets.

@juniorthiesen
Created March 24, 2021 16:58
Show Gist options
  • Select an option

  • Save juniorthiesen/bbb6501777253e8a4587afab5096a60f to your computer and use it in GitHub Desktop.

Select an option

Save juniorthiesen/bbb6501777253e8a4587afab5096a60f to your computer and use it in GitHub Desktop.
Get the product brand term
// Utility: Get the product brand term names (from the product ID)
// https://wordpress.org/plugins/perfect-woocommerce-brands/
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;
}
@outofhouse
Copy link

This is really useful and much appreciated! Have utilised on a client's store and working brilliantly.
One question - is there a way of editing the above to only show parent brand? It would be useful to be able to have child brands for display purposes on the site but only show the parent brand in cart, checkout and on order emails & notifications...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment