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.

importantMake 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 (WPT_PBS)
 *
 * https://wp-tutorials.tech/refine-wordpress/add-brand-to-woocommerce-product-schema/
 */

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

/**
 * If you've used a different product attribute like "Marque" instead of
 * "Brand", change it here.
 */
const WPT_PBS_BRAND_ATTRIBUTE = 'pa_brand';
// const WPT_PBS_BRAND_ATTRIBUTE = 'pa_marque';
// const WPT_PBS_BRAND_ATTRIBUTE = 'pa_marca';

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(), WPT_PBS_BRAND_ATTRIBUTE, 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..

Test a page's structured data
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!

Confirm that "brand" is in the JSON data
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?

loading

Let us know

13 thoughts on “Add Brand to WooCommerce Product Schema”

  1. Avatar

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

    This gives error on file location. Crashes the site.

    Why can’t i put contents of functions-brand-schema.php as snippet via custom snippets plugin?

    1. Avatar

      This tutorial shouldn’t crash your site at all. It’s well tested – I use it on several sites.
      I usually recommend using a custom child theme so we can enqueue extra CSS/JS files, but this tutorial only uses PHP, so… yes. You absolutely can use a custom snippets plugin.

      1. Avatar

        Thanks for reply. So I don’t need to use as a separate php file but creating a snippet and put those content of files would be enough if i use custom snippets plugin. Right?

        1. Avatar

          With this tutorial, that is correct.
          Just make sure you test it with the Structured Data Tester to verify that it is working correctly.

    1. Avatar

      Hi Fabian
      I’ve adjusted the tutorial so you can use a product attribute called “Marque”, but still put “Brand” into the schama. It looks good in my testing. Try the new code and let me know if it works for you too.

  2. Avatar

    Good tip but the validator isn’t the same as the Google Products URL inspection tool which returns errors when the validator does not.

    1. Avatar

      Hi Bard
      The Schema Markup Validator tests the live site. The Product URL Validator in Search Console tests what’s in Google’s index, which might be two weeks old – if that’s what you are referring to. In my experience, if you get it working in the structured data testing tool (the red one) then it will work in Google Search Console in one or two weeks time, depending on how many signals Google is receiving about your site – busier sites get re-indexed more frequently.
      …unless I’ve misunderstood your query?

  3. Avatar

    Hello Headwall,

    Great tutorial, thanks a lot!

    Would it be possible to add the GTIN as well?
    To fix this as well: Warning No global identifier provided (e.g., gtin, mpn, isbn)

    I tried to replace all “Brand” with “GTIN” in the code provided, but that does not seem to work…

    Looking forward to your reply πŸ™‚

    1. Avatar

      Hi Victor

      I’m glad the tutorial is helpful for you.

      GTIN/EAN/MPN/ISBN works differently to Brand:

      * Brand is a taxonomy, and a “taxonomy” is a way of grouping related content. Multiple content can share the same terms.
      * GTIN/EAN/…. are globally unique identifies.

      I’ve written code to do this for a client, and it works really well. But it’s a bit fiddly, as it needs to work with products and product-variations.

      I’ll flag it as my next tutorial to write-up, but it won’t be for a few days as I’ve got some “proper work” to get sorted for a client, for next week.

      Follow “Headwall Hosting” on Facebook (or @HeadwallHosting on Twitter) for alerts when new tutorials are posted, or check back here in about a week, I reckon.

      Paul

Leave a Comment

Your email address will not be published.