Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

07. Craft Commerce Product Microdata

khalwat edited this page May 30, 2016 · 4 revisions

Craft Commerce Product Microdata

If an SEOmatic FieldType is attached to a Craft Commerce Product, in addition to rendering the page SEO Meta, it will also generate Product JSON-LD microdata that describes the product.

It does this by pulling values from the seomaticMeta settings from the SEOmatic FieldType, as well as by pulling data from the Craft Commerce Product. If you have an SEOmatic FieldType attached to a Craft Commerce Product, a new seomaticProduct array is injected into your page template:

{% set seomaticProduct = [
    {
        type: "Product",
        name: "Romper for a Red Eye",
        description: "Irresistible to women.  Establishes dominance over men.  Brad's for Men will release your inner beast with its musky essence.",
        image: "http://bradsformen.dev/img/site/site_logo.png",
        logo: "http://bradsformen.dev/img/site/site_logo.png",
        url: "http://bradsformen.dev/commerce/products/romper-for-a-red-eye",
        sku: "Romper for a Red Eye",
        offers: {
            type: "Offer",
            url: "http://bradsformen.dev/commerce/products/romper-for-a-red-eye",
            price: "30.00",
            priceCurrency: "USD",
            offeredBy: seomaticIdenity,
            seller: seomaticIdenity,
        }
    }
] %}

Since this is just a Twig array, you can alter it as you see fit, and whatever changes you make will be reflected in the JSON-LD that SEOmatic renders via the {% hook 'seomaticRender' %} Because of the way that Twig handles arrays, you must include every field in the array when doing a set or merge, otherwise the fields you exclude will not exist.

Or if you want to set just one variable in the array, you can use the Twig function merge:

{% set seomaticProduct = seomaticProduct | merge({'brand': entry.brandInfo }) %}

You can change these seomaticProduct variables in your templates that extends your main layout.twig template, and due to the Twig rendering order, when {% hook 'seomaticRender' %} is called, they'll be populated in your rendered SEO Meta tags.

If you do not want SEOmatic to automatically render Craft Commerce Product JSON-LD, you can use the renderCommerceProductJSONLD config variable (see the config.php file for SEOmatic).

See the section Dynamic Twig SEO Meta for more information on how to manipulate SEOmatic variables via Twig.

SEOmatic also automatically strips HTML/PHP tags from the variables, and translates HTML entities to ensure that they are properly encoded.