WC_Brands::add_structured_data
Add structured data to product page.
Method of the class: WC_Brands{}
No Hooks.
Returns
Array. $markup
Usage
$WC_Brands = new WC_Brands(); $WC_Brands->add_structured_data( $markup, $product );
- $markup(array) (required)
- Markup.
- $product(WC_Product|null)
- Product data.
Default:null
WC_Brands::add_structured_data() WC Brands::add structured data code WC 11.0.1
public function add_structured_data( $markup, $product = null ) {
global $post;
if ( ! is_array( $markup ) ) {
$markup = array();
}
if ( array_key_exists( 'brand', $markup ) ) {
return $markup;
}
$product_id = $product instanceof WC_Product ? $product->get_id() : ( isset( $post->ID ) ? $post->ID : 0 );
if ( ! $product_id ) {
return $markup;
}
$brands = get_the_terms( $product_id, 'product_brand' );
if ( ! empty( $brands ) && is_array( $brands ) ) {
// Can only return one brand, so pick the first.
$brand_thumbnail = wc_get_brand_thumbnail_url( $brands[0]->term_id, 'full' );
$markup['brand'] = array(
'@type' => 'Brand',
'name' => $brands[0]->name,
);
if ( $brand_thumbnail ) {
$markup['brand']['logo'] = $brand_thumbnail;
}
}
return $markup;
}