Home / Admin / Use a WooCommerce Product Attribute as the Brand in Product Schema
Duplicate Snippet

Embed Snippet on Your Site

Use a WooCommerce Product Attribute as the Brand in Product Schema

This filter enables using a WooCommerce Product Attribute as the Brand in our Product Schema.

Note: replace pa_brand with the attribute name.

<10
Code Preview
php
<?php
add_filter( 'aioseo_schema_output', 'aioseo_filter_schema_output' );
function aioseo_filter_schema_output( $graph ) {
    if ( ! function_exists( 'is_product' ) || ! is_product() ) {
        return $graph;
    }
    foreach ( $graph as &$item ) {
        if ( 'Product' !== $item['@type'] ) {
            continue;
        }
        if ( ! empty( $item['brand'] ) ) {
            break;
        }
        $product = wc_get_product();
        $brand   = $product->get_attribute( 'pa_brand' );
        if ( ! empty( $brand ) ) {
            $item['brand'] = [
                '@type' => 'Brand',
                'name'  => $brand
            ];
        }
    }
    return $graph;
}

Comments

Add a Comment