Your cart is currently empty!
Show brand attribute on single product short descriptions
Displays the brand attribute linked on single product pages under the short descriptions.
add_action( 'woocommerce_before_add_to_cart_form', function() {
// Get Brand Attribute Value
global $product;
$brand = $product->get_attribute( 'pa_brand' );
// Skip If Empty
if( ! $brand ) {
return;
}
// Loop Attributes
foreach( $product->get_attributes() as $attribute ) {
// Match Brand
if( $attribute['name'] != 'pa_brand' ) {
continue;
}
// Get Taxonomy And Values
$attribute_taxonomy = $attribute->get_taxonomy_object();
$attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), [ 'fields' => 'all' ] );
// Loop Attribute Values
$brands = [];
foreach( $attribute_values as $attribute_value ) {
if( ! $attribute_taxonomy->attribute_public ) {
continue;
}
$brands[] = sprintf(
'<a href="%s" rel="tag">%s</a>',
esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ),
esc_html( $attribute_value->name )
);
}
// Output
if( $brands ) {
printf(
'<p><strong>Brand:</strong> %s</p>',
implode( ', ', $brands )
);
}
}
} );
Instructions for Show brand attribute on single product short descriptions
- Log into a staging or locally hosted clone of your site.
- Install and activate Code Snippets plugin.
- WP Admin > Snippets > Add New
- Copy and paste the code from the section above.
- Check to ensure formatting came over properly.
- Customize the code as desired.
- Add a meaningful title.
- Select whether to run on front-end or back-end (or both).
- Click “Save and Activate”.
- Test your site to ensure it works.
- Disable if any problems, or recover.
- Repeat for live environment.
Need help modifying Show brand attribute on single product short descriptions?
Contact me. I can help with fitting projects or refer to my partner.
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.
Disclaimer of warranty