Add Brand to WooCommerce Product Schema

Brand schema tutorial

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.

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 : Add "brand" to the product 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.

tipIf 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..

Sstructured data testing tool
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!

Social brand schema
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

WordPress plugins for developers

24 thoughts on “Add Brand to WooCommerce Product Schema”

  1. // 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?

    Reply
    • 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.

      Reply
      • 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?

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

    Reply
    • 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?

      Reply
  3. 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 🙂

    Reply
    • 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

      Reply
      • Hi Paul, thanks for your reply! I’ll be looking patiently forward to your next tutorial 🙂 I’ll keep an eye on your site!

        Reply
      • Hi Paul, do you maybe have an update on the next tutorial about the GTIN? A code that works only for simple product would already be very welcome 🙂

        Reply
        • Sorry for the delay. I’ve been really busy with our power-plugins.com project. That said, I have written the code for the GTIN/EAN tutorial. All I need to do is write it up. I’ll get it online over the next few days 👍

          Reply
  4. I’ve entered the first box of code above via a snippets plugin. I am seeing the values I believe, but it is not being schema as “brand”.

    It is saying:

    additionalProperty
    @type PropertyValue
    name pa_brand
    value (the value I added in for brand is working)

    So instead of brand, it is saying “additional property”. I am wondering if I am doing something wrong. If it doesn’t work with the snippet editor, then I will revert to your method used on this tutorial, but was curious if you knew how to address this.

    Reply
    • It looks like something’s not quite right here. I’ve just rechecked another site that’s using the code and it seems to work OK. I was wondering if recent WooCommerce updates had broken something.

      Looking at your schema snippet, I don’t see how you’ve got ‘@type’ => ‘PropertyValue’. That doesn’t relate to anything in the code snippet. The code snippet has ‘@type’ => ‘Brand’.

      So… if you remove the code snippet, do you see something different? Have you flushed the page cache after making the changes, and before you check with the Structured Data Testing Tool?

      I’m happy to help you out directly if needs be – it’s a great little code snippet for SEO.

      Reply
      • I’ve narrowed down that the your code is not active, I added another attribute (colour) and it also has the “propertyvalue” also. So the code snippet plugin is not correctly using the code, or maybe is not compatible with Astra or Elementor, or I have made a mistake somehow but it seems fairly straight forward…

        Should “add_filter(‘woocommerce_structured_data_product’, ‘add_brand_to_product_schema’);” be at the top of the code and not the bottom for the code snippet? I am not a coder, but I don’t see any filters where this command as the bottom, not sure if it matters. We can take this to email if you wish no problem!

        Reply
        • Sorry for the delay in getting back to you.

          You might be on to something there, but moving the code to the top/bottom of the PHP file won’t make a difference. What you might want to do is adjust the filter priority, like this:

          add_filter(‘woocommerce_structured_data_product’, ‘add_brand_to_product_schema’, PHP_INT_MAX);

          That means the add_brand_to_product_schema() gets called later in the hook-cycle. It’s definitely worth a try.

          Reply
  5. With Google’s new product update the new Merchant listings are receiving an error for – Missing field “priceType” it appears to be a new thing on all woocommerce products that’s causing invalid products in GSC

    Reply
    • I’m not seeing that on my WooCommerce sites in Search Console. We’ve got Offers, which themselves have a priceSpecification member. If this is an in-progress change then I’ll keep an eye out for it in Search Console 👍

      Reply
  6. Dear Headawall,

    How to fix Missing Field “shpping details” and “merchantreturnpolicy” ? Please email me about that.

    Thank you so much

    Reply
    • Adding these two fields to the tutorial is on my todo-list. I haven’t decided whether to extend this tutorial, ro maybe create a new tutorial. I will write the update soon though. If you follow me on Facebook, you will see when I post the new code.

      Reply

Leave a comment