Nick
Nick

Reputation: 1

How to add "Brand" element to schema markup (WooCommerce microdata)

Is there a possibility to add information about product brand in WooCommerce product schema?

I tried to use product schema plugins, but they don't fit my needs and break existing schema or add a second one to the product code, so the best solution is to modify it manually. I tried to use this snippet, but it doesn't create the correct markup structure:

add_filter( 'woocommerce_structured_data_product_offer', 'nt_woocommerce_structured_data_product_offer', 10, 2 );

function nt_woocommerce_structured_data_product_offer( $markup, $product ) {


    $markup[ 'brand' ] = wc_get_product()->get_attribute('pa_brand');

    return $markup;

}

I need this snippet to create something like this:

"brand": {
    "@type": "Thing",
    "name": "ACME"
  }

but it creates such a markup, which is not validated by Google:

"brand":"ACME"

Do you have any ideas about how to create correct markup by using a php snippet?

Upvotes: 0

Views: 1087

Answers (1)

xboltik
xboltik

Reputation: 11

You need to use a different filter woocommerce_structured_data_product and set the brand using an array.

add_filter( 'woocommerce_structured_data_product', 'nt_woocommerce_structured_data_product_offer', 10, 2 );
function nt_woocommerce_structured_data_product_offer( $markup, $product ) {

    $markup[ 'brand' ] = array(
        '@type'  => 'Thing',
        'name'   => wc_get_product()->get_attribute('pa_brand'),
    );

    return $markup;
}

Upvotes: 1

Related Questions