Skip to content

Instantly share code, notes, and snippets.

@Auke1810
Last active January 16, 2019 09:18
Show Gist options
  • Select an option

  • Save Auke1810/9ce12d8ee072897db2f5186e232c3102 to your computer and use it in GitHub Desktop.

Select an option

Save Auke1810/9ce12d8ee072897db2f5186e232c3102 to your computer and use it in GitHub Desktop.
Easily add the correct structured data using JSON-LD Google Merchant, this includes the missing microdata for condition.
<?php
// Locate the file header.php in your theme
// Before editing header.php, always make a backup so that you can roll back if something goes wrong.
// At the bottom of the header, simple add the code below
// Verify that you have done it correctly by checking the page with Google’s Structured Data Testing Tool
// https://search.google.com/structured-data/testing-tool
if (isset($product)){
$meta = get_post_meta(get_the_ID());
$_product = new WC_Product(get_the_ID());
if ($_product->regular_price!=NULL){
$price = $_product->regular_price;
}elseif ($_product->price!=NULL){
$price = $_product->price;
}
if ( ($_product->price > $_product->sale_price) && ($_product->sale_price!=NULL) ){
$price = $_product->sale_price;
}
?>
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "<?php echo get_the_title(get_the_ID()); ?>",
"url": "<?php echo get_permalink( get_the_ID() ); ?>",
"description": "<?php echo $_product->get_description(); ?>",
"image": "<?php echo $_product->get_image_id(); ?>",
"aggregateRating": "",
"brand": "",
"review": "",
"sku": "<?php echo $_product->get_sku(); ?>",
"offers": {
"@type": "Offer",
"priceCurrency": "<?php echo get_woocommerce_currency(); ?>",
"price": "<?php echo $price; ?>",
"itemCondition" : "http://schema.org/NewCondition",
"availability" : "http://schema.org/<?php echo $meta['_stock_status'][0] ? 'InStock' : 'OutOfStock'; ?>"
}
}
</script>
<?php } ?>
@Auke1810

Auke1810 commented Jan 16, 2019

Copy link
Copy Markdown
Author

Added url and description.
image, aggregateRating, brand, review, and "sku"

Some are not populated because it will depend on wordpress / woocommerce setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment