Tag Archive for: WordPress

Adding more structured data to WooCommerce products

A theme I was using had the notion of “brand” built into it, however this wasn’t being added to the WooCommerce structured data.

It’s quite simple to do.  Whilst at it, I added the product gallery images.

See the code..

add_filter( 'woocommerce_structured_data_product', function( $markup, $product ) {

  // add Brand
  if($brand = $product->get_attribute( 'pa_brand' )) {
    $markup['brand'] = $brand;
  } 

  // product gallery images
  if($attachment_ids = $product->get_gallery_image_ids()) {
    $gallery_images = array($markup['image']);
    
    foreach( $attachment_ids as $attachment_id ) {
      $gallery_images[] = wp_get_attachment_url( $attachment_id );
    }

    $markup['image'] = $gallery_images;
  }

  return $markup;
}, 10, 2 );

For my theme the brand attribute was named pa_brand.  Rename this for your purposes.

Once you’ve generated your structured data you can see what google thinks of it on their rich results test.

 

Tag Archive for: WordPress