Add the Brand to Product Structured Data
Includes the product brand in the product schema WooCommerce outputs, which Google uses for rich results and Merchant listings.
Opens WPIDE on
Code
add_filter( 'woocommerce_structured_data_product', function ( $markup, $product ) {
$default_brand = get_bloginfo( 'name' ); // Used when the product has no brand.
if ( ! empty( $markup['brand'] ) ) {
return $markup;
}
$brand = '';
if ( taxonomy_exists( 'product_brand' ) ) {
$terms = get_the_terms( $product->get_id(), 'product_brand' );
if ( $terms && ! is_wp_error( $terms ) ) {
$brand = $terms[0]->name;
}
}
$brand = $brand ? $brand : $default_brand;
if ( $brand ) {
$markup['brand'] = array(
'@type' => 'Brand',
'name' => wp_strip_all_tags( $brand ),
);
}
return $markup;
}, 10, 2 );
Notes
Uses the WooCommerce Brands taxonomy when it exists, otherwise falls back to $default_brand.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.