Skip to content

Commit

Permalink
structured data snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
tristantbg committed Nov 20, 2020
1 parent 3543d69 commit 9db67e0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
'pages/shopify.collections' => __DIR__ . '/src/blueprints/shopify.collections.yml',
'pages/shopify.collection' => __DIR__ . '/src/blueprints/shopify.collection.yml'
],
'snippets' => [
'kirby-shopify.product.structured-data' => __DIR__ . '/src/snippets/product.structured-data.php'
],
'fieldMethods' => [
'img_url' => function ($field, string $size = '') {
if (is_string($field)) {
Expand Down
48 changes: 48 additions & 0 deletions src/snippets/product.structured-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "<?= $page->shopifyTitle()->escape() ?>",
"url": "<?= $page->url() ?>",
<?php if ($productImages->count()): ?>
"image": [
"<?= $productImages->first()->src() ?>"
],
<?php endif ?>
"description": "<?= Escape::html(Str::unhtml($page->shopifyDescriptionHTML())) ?>",
"brand": {
"@type": "Thing",
"name": "<?= $page->shopifyVendor()->escape() ?>"
},
<?php if ($page->shopifyVariants()->toStructure()->count()): ?>
"offers": [
<?php foreach ($page->shopifyVariants()->toStructure() as $key => $variant): ?>
{
"@type" : "Offer",
"availability" : "http://schema.org/<?= r($page->isAvailable(), 'InStock', 'OutOfStock') ?>",
"price" : "<?= $variant->price() ?>",
"priceCurrency" : "EUR",
"url" : "<?= $variant->url() ?>",
"itemOffered" :
{
"@type" : "Product",
"name" : "<?= $variant->title()->escape() ?>",
<?php if ($variant->sku()->isNotEmpty()): ?>
"sku": "<?= $variant->sku() ?>",
<?php endif ?>
<?php if ($variant->weight()): ?>
"weight": {
"@type": "QuantitativeValue",
<?php if ($variant->weight_unit()->isNotEmpty()): ?>
"unitCode": "<?= $variant->weight_unit() ?>",
<?php endif ?>
"value": "<?= $variant->weight().r($variant->weight_unit()->isNotEmpty(), ' '.$variant->weight_unit()) ?>"
},
<?php endif ?>
}
},
<?php endforeach ?>
]
<?php endif ?>
}
</script>

0 comments on commit 9db67e0

Please sign in to comment.