Add Brand to WooCommerce Product Schema

Learn how to fix the Google Search Console warning, “Missing field ‘brand‘” by adding a product’s brand to the structured data (schema) in WooCommerce.

This is an easy to follow tutorial that doesn’t need any new plugins. We’ll use some standard WooCommerce functionality to assign brands to products, and we’ll write a bit of PHP to inject the brands into the site’s structured data.

infoMake sure you’re using a custom child theme, so you can edit functions.php.

Brand schema tutorial
Search Console Warning

Missing Field “Brand”

Google Search Console is a valuable tool when you’re trying to improve a website’s organic search performance. WooCommerce marks-up product pages with lots of juicy data, and you can examine it yourself. Pick any WooCommerce product page and paste the URL into Google’s Structured Data Testing tool (the Schema Markup Validator).

Google structured data testing tool
Google’s “Schema Markup Validator” structured data on live web pages

Create a Brand Product Attribute

The first thing we need to do is create a way to assign brands to our products. We’ll use WooCommerce’s built-in Product Attributes to do this. In your site’s admin area, go to Products > Attributes and create a new attribute called “Brand”.

WooCommerce product attributes
Our “brand” product attribute.

Next, create some terms (brands) and assign them to your actual products.

Add a brand to a WooCommerce product.
Add a brand to your products.

Modify the Product Schema

We’re going to create a small PHP function to detect when a product has a Brand attribute, and append the structured data accordingly.

In your custom child theme’s folder, create a file called functions-brand-schema.php and paste the following into it.

<?php

/**
 * WP Tutorials : Product brand schema
 *
 * https://wp-tutorials.tech/refine-wordpress/add-brand-to-woocommerce-product-schema/
 */

// Block direct access.
defined('WPINC') || die();

function add_brand_to_product_schema($data) {
	if (!is_array($data)) {
		// If $data isn't an array, don't do anything.
	} elseif (empty($product = wc_get_product())) {
		// Don't do anything.
	} elseif (!is_array($brands = wc_get_product_terms($product->get_id(), 'pa_brand', array('fields' => 'names')))) {
		// This product has no brands associated with it.
	} elseif (empty($brands)) {
		// This product has zero brands associated with it.
	} elseif (count($brands) == 1) {
		// This product has exactly one brand associated with it.
		$data['brand'] = array('@type' => 'Brand', 'name' => $brands[0]);
	} else {
		// This product has multiple brands associated with it.
		$data['brand'] = array();
		foreach ($brands as $brand) {
			$data['brand'][] = array('@type' => 'Brand', 'name' => $brand);
		}
	}

	return $data;
}
add_filter('woocommerce_structured_data_product', 'add_brand_to_product_schema');

All we’re doing here is hooking a standard WooCommerce filter called woocommerce_structured_data_product, which returns an array of structured product data, as per the Schema.org Product specification.

Next, open your child theme’s functions.php and paste the following into it, to pull-in our new function.

// WP Tutorials : Product Brand Schema
require_once dirname(__FILE__) . '/functions-brand-schema.php';

That should be it working. Now all we need to do is test it…

Validate the Brand Schema

Open a new browser tab and go to the Structured Data Testing Tool. Paste the URL of a single product page that has a brand associated with it.

infoIf you’re using a page caching plugin then you should flush the cache, to be sure you don’t accidentally validate an old version of the page..

Live test a URL for schema

You’ll see that the tool picks up all sorts of structured data elements, and one of them should be our Product object. Select/expand the Product object and scroll down the list of fields. You should see Brand in there!

Product Brand Schema

Now it’s confirmed to be working, you can (re)submit your product URLs to Google Search Console. After a week-or-so, you should see the “Missing Field, Brand” warning disappear, and your products should be more visible on Google Search.

Happy branding! 😎👍

Like This Tutorial?

Let us know

Leave a Comment

Your email address will not be published. Required fields are marked *