To add Product schema in Astro, render a Product JSON-LD block on each product page with name, image, description and an offers object holding price, priceCurrency, availability and url. Build it from a products content collection so the structured data always matches the price shown on the page, and render it with <script type="application/ld+json" set:html={...}>.
Key Takeaways
- Product pages need
nameplus at least one ofoffers,revieworaggregateRatingfor product rich results.- Keep price and availability in one typed data source used by both the page and the schema.
availabilitytakes a schema.org URL such ashttps://schema.org/InStock.- Only add ratings that come from real reviews shown on the page.
A products collection
// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';
const products = defineCollection({
loader: glob({ pattern: '**/*.md', base: './src/content/products' }),
schema: ({ image }) =>
z.object({
name: z.string(),
description: z.string(),
sku: z.string(),
brand: z.string(),
price: z.number().positive(),
currency: z.string().length(3).default('USD'),
inStock: z.boolean().default(true),
photo: image(),
}),
});
export const collections = { products };
The schema component
---
// src/components/ProductSchema.astro
import type { CollectionEntry } from 'astro:content';
import { getImage } from 'astro:assets';
interface Props {
product: CollectionEntry<'products'>;
}
const { product } = Astro.props;
const p = product.data;
const url = new URL(Astro.url.pathname, Astro.site).href;
const photo = await getImage({ src: p.photo, width: 1200, format: 'jpg' });
const schema = {
'@context': 'https://schema.org',
'@type': 'Product',
name: p.name,
description: p.description,
sku: p.sku,
brand: { '@type': 'Brand', name: p.brand },
image: [new URL(photo.src, Astro.site).href],
offers: {
'@type': 'Offer',
url,
price: p.price.toFixed(2),
priceCurrency: p.currency,
availability: p.inStock ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
itemCondition: 'https://schema.org/NewCondition',
},
};
const json = JSON.stringify(schema).replace(/</g, '\\u003c');
---
<script type="application/ld+json" set:html={json} />
Add <ProductSchema product={entry} /> to the product page’s <head>. getImage() gives you the optimized image URL, and new URL(..., Astro.site) makes it absolute, which requires site in your config. Escaping < keeps a product name with angle brackets from breaking the script tag. The config docs show where site is set.
Price, currency and availability
| Property | Value |
|---|---|
price |
A number or numeric string without a currency symbol, such as "29.00" |
priceCurrency |
ISO 4217 code: USD, EUR, GBP |
availability |
https://schema.org/InStock, OutOfStock, PreOrder, BackOrder and others |
priceValidUntil |
Optional date for sale prices |
If a product has variants with different prices, use an AggregateOffer with lowPrice, highPrice and offerCount, or a ProductGroup with one Product per variant.
For physical goods sold directly, Google’s merchant listing features also look at shippingDetails and hasMerchantReturnPolicy. Digital products, such as software or website themes, can leave out shipping.
Reviews and ratings
aggregateRating and review can earn star ratings in results, but only when they reflect genuine reviews that visitors can see on the page. Do not hard-code a rating, copy ratings from another site, or mark up reviews that the business wrote about itself. If you have no reviews yet, leave these properties out; the offer is enough for price and availability.
Test it
Run a deployed product page through Google’s Rich Results Test, which reports product snippet and merchant listing eligibility. Search Console then shows Product reports once Google has crawled the pages. For a static store, the price in the schema updates when you rebuild, so rebuild whenever a price changes. Our JSON-LD guide covers combining Product with BreadcrumbList, and Astro e-commerce options compares checkouts that pair with a static product catalog. A theme product page in our catalog is an example of a digital product page with a single price.
FAQ
What fields are required for Product schema?
Google requires name and at least one of offers, review or aggregateRating for product snippets. For price display, include offers with price, priceCurrency and availability.
Can I use Product schema on a static Astro store?
Yes. Structured data is part of the HTML, so it works the same for prerendered pages. Rebuild the site when prices or stock change so the markup stays accurate.
Should the schema price match the visible price?
Yes. A mismatch between the markup and the page can make the markup ineligible for rich results. Render both from the same data.
Can I add Product schema to a page with several products?
Product rich results are meant for pages about one product. For category pages, link to individual product pages instead of marking up every item.