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

Andrew Welch edited this page Apr 18, 2019 · 4 revisions

No Maintenance Intended

DEPRECATED

This Craft CMS 2.x plugin is no longer supported, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons.

The Craft CMS 3.x version of this plugin can be found here: craft-seomatic and can also be installed via the Craft Plugin Store in the Craft CP.

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, seomaticMainEntityOfPage array is injected into your page template:

{% set seomaticMainEntityOfPage = [
    {
        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. Because this is an array of Products, you need to do something like this to add to each Product in the array:

{% if seomaticMainEntityOfPage is defined %}
    {% set productBrand = {
        'type': 'thing',
        'name': product.title
        }
    %}
    {% set tempMainEntityOfPage = [] %}
    {% for productJsonLd in seomaticMainEntityOfPage %}
        {% set productJsonLd = productJsonLd | merge({"brand": productBrand }) %}
        {% set tempMainEntityOfPage = tempMainEntityOfPage |merge([productJsonLd])  %}
    {% endfor %}
    {% set seomaticMainEntityOfPage = tempMainEntityOfPage %}
{% endif %}

You can change these seomaticMainEntityOfPage 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.