Skip to content

Instantly share code, notes, and snippets.

@serkanalgur
Created June 15, 2022 09:29
Show Gist options
  • Save serkanalgur/20a17e9afe63dbea5ad2330d8e475a86 to your computer and use it in GitHub Desktop.
Save serkanalgur/20a17e9afe63dbea5ad2330d8e475a86 to your computer and use it in GitHub Desktop.
Product Schema for WooCommerce
<?php
add_action( 'wp_head', 'add_product_info_to_head' );
function add_product_info_to_head() {
global $product;
if ( is_product() ) {
$image = wp_get_attachment_image_url( $product->get_image_id(), 'full', false );
echo '<script type="application/ld+json">
{
"@context": "https://schema.org/",
"@type": "Product",
"name": "' . $product->get_name() . '",
"image": ["' . $image . '"],
"description": "' . ($product->get_short_description() != ''? $product->get_short_description(): $product->get_formatted_name()) . '",
"sku": "' . $product->get_sku() . '",
"mpn": "' . $product->get_sku() . '",
"brand": {
"@type": "Brand",
"name": "Not A Brand"
},
"review": {
"@type": "Review",
"reviewRating": {
"@type": "Rating",
"ratingValue": "4",
"bestRating": "5"
},
"author": {
"@type": "Person",
"name": "Not A Person"
}
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "4.4",
"reviewCount": "89"
},
"offers": {
"@type": "Offer",
"url": "' . $product->get_permalink() . '",
"priceCurrency": "AZN",
"price": "' . $product->get_price() . '",
"priceValidUntil": "2030-12-31",
"itemCondition": "https://schema.org/NewCondition",
"availability": "'.('instock' ===$product->get_stock_status() ? "https://schema.org/InStock" : "https://schema.org/OutOfStock").'"
}
}
</script>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment