Skip to content

Instantly share code, notes, and snippets.

@andrew-serrano
Last active September 11, 2020 14:58
Show Gist options
  • Save andrew-serrano/bc32f56f6b47efcc51a4fa2537140bb2 to your computer and use it in GitHub Desktop.
Save andrew-serrano/bc32f56f6b47efcc51a4fa2537140bb2 to your computer and use it in GitHub Desktop.
JSON-LD Product schema base
<!-- Start: Google Rich Snippets -->
<mvt:comment>
| Check if the argument is a structure to transform into an array
</mvt:comment>
<mvt:if expr="tolower(miva_variable_type(l.settings:json_ld:products)) EQ 'structure'">
<mvt:assign name="l.null" value="miva_array_insert( l.settings:json_ld:products, l.settings:json_ld:products, -1 )" />
</mvt:if>
<mvt:comment>
| Iterate over products
</mvt:comment>
<mvt:foreach iterator="product" array="json_ld:products">
<mvt:comment>
| Defualts
</mvt:comment>
<mvt:comment>
| Image
</mvt:comment>
<mvt:if expr="ISNULL g.socialImage">
<mvt:do file="g.Module_Library_DB" name="l.success" value="ImageType_Load_Code('main', l.imagetype)" />
<mvt:do name="l.success" file="g.Module_Library_DB" value="ProductImage_Load_Type(l.settings:product:id, l.imagetype:id, l.imagetype)" />
<mvt:do name="l.success" file="g.Module_Library_DB" value="Image_Load_ID(l.imagetype:image_id, l.imagedata)" />
<mvt:assign name="g.socialImage" value="g.baseurl $ l.imagedata:image" />
</mvt:if>
<mvt:comment>
| Strip any HTML from the product description.
</mvt:comment>
<mvt:assign name="l.settings:product:stripped_descrip" value="encodeentities(miva_html_strip(l.settings:product:descrip, ''))" />
<mvt:comment>
| JSON+LD : Product
</mvt:comment>
<mvt:assign name="l.json_ld:@context" value="'https://schema.org/'" />
<mvt:assign name="l.json_ld:@type" value="'Product'" />
<mvt:assign name="l.json_ld:name" value="encodeentities(l.settings:product:name)" />
<mvt:assign name="l.json_ld:category" value="encodeentities(l.settings:category:name)" />
<mvt:assign name="l.json_ld:description" value="encodeentities(l.settings:product:stripped_descrip)" />
<mvt:comment>
| SKU
</mvt:comment>
<mvt:if expr="NOT ISNULL l.settings:product:sku">
<mvt:assign name="l.json_ld:mpn" value="encodeentities(l.settings:product:sku)" />
<mvt:assign name="l.json_ld:sku" value="encodeentities(l.settings:product:sku)" />
<mvt:else>
<mvt:assign name="l.json_ld:mpn" value="encodeentities(l.settings:product:code)" />
<mvt:assign name="l.json_ld:sku" value="encodeentities(l.settings:product:code)" />
</mvt:if>
<mvt:comment>
| Image
</mvt:comment>
<mvt:if expr="NOT ISNULL g.socialImage">
<mvt:assign name="l.json_ld:image" value="g.socialImage" />
</mvt:if>
<mvt:comment>
| Brand
</mvt:comment>
<mvt:if expr="NOT ISNULL l.settings:product:customfield_values:customfields:brand">
<mvt:assign name="l.json_ld:brand" value="encodeentities(l.settings:product:customfield_values:customfields:brand)" />
<mvt:else>
<mvt:assign name="l.json_ld:brand" value="encodeentities(g.store:name)" />
</mvt:if>
<mvt:comment>
| JSON+LD : Product : Offer
</mvt:comment>
<mvt:assign name="l.json_ld:offers:@type" value="'Offer'" />
<mvt:assign name="l.json_ld:offers:url" value="l.settings:product:link" />
<mvt:assign name="l.json_ld:offers:priceCurrency" value="'USD'" />
<mvt:assign name="l.json_ld:offers:seller" value="encodeentities(g.store:name)" />
<mvt:assign name="l.json_ld:offers:itemCondition" value="'new'" />
<mvt:comment>
| SKU
</mvt:comment>
<mvt:if expr="NOT ISNULL l.settings:product:sku">
<mvt:assign name="l.json_ld:offers:sku" value="encodeentities(l.settings:product:sku)" />
<mvt:else>
<mvt:assign name="l.json_ld:offers:sku" value="encodeentities(l.settings:product:code)" />
</mvt:if>
<mvt:comment>
| Inventory
</mvt:comment>
<mvt:if expr="l.settings:attributemachine:product:inv_level">
<mvt:assign name="l.availability_inv_level" value="l.settings:attributemachine:product:inv_level" />
<mvt:do name="l.formatted_variant_price" file="g.Module_Root $ g.Store:currncy_mod:module" value="CurrencyModule_AddFormatting( g.Store:currncy_mod, l.settings:attributemachine:product:price )" />
<mvt:assign name="l.json_ld:offers:price" value="l.settings:attributemachine:product:price" />
<mvt:assign name="l.json_ld:offers:eligibleQuantity" value="l.settings:attributemachine:product:inv_available" />
<mvt:else>
<mvt:assign name="l.availability_inv_level" value="l.settings:product:inv_level" />
<mvt:assign name="l.json_ld:offers:price" value="l.settings:product:price" />
<mvt:assign name="l.json_ld:offers:eligibleQuantity" value="l.settings:product:inv_available" />
</mvt:if>
<mvt:if expr="l.availability_inv_level EQ 'low'">
<mvt:assign name="l.json_ld:offers:availability" value="'LimitedAvailability'" />
<mvt:elseif expr="l.availability_inv_level EQ 'out'">
<mvt:assign name="l.json_ld:offers:availability" value="'OutOfStock'" />
<mvt:else>
<mvt:assign name="l.json_ld:offers:availability" value="'InStock'" />
</mvt:if>
<mvt:comment>
| Insert into array that will be outputted
</mvt:comment>
<mvt:assign name="l.null" value="miva_array_insert( l.json_ld_items, l.json_ld, -1 )" />
</mvt:foreach>
<mvt:comment>
| JSON+LD output
</mvt:comment>
<script type="application/ld+json">
<mvt:do file="g.Module_JSON" name="l.success" value="JSON_Output( l.json_ld_items )" />
</script>
<!-- End: Google Rich Snippets -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment