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 )
);
}
}
} );
Show brand attribute on single product short descriptions
Displays the brand attribute linked on single product pages under the short descriptions.
How to use
- Log into a staging, development, or locally hosted clone of your site
- Install and activate Code Snippets
- WP Admin > Snippets > Add New
- Copy and paste the code from the Description tab above
- Check to ensure formatting came over properly and no syntax errors show up in the editor
- 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
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
Disclaimer of warranty:
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.
Support
- Describe the issue and what you’ve observed.
- Describe your expected outcome(s).
- List steps to reproduce the issue.
- Optionally provide screen-shot or video URLs.
- Email to [email protected]
