SEO Partial parameters from JSON file

Hello All,

So I’ve been trying to get our SEO partial to auto populate/pull the price values set within the product JSON files.

Solution Ideas:

  1. Pass only the required values from within $products OR directly from the .JSON data file. This is the most ideal option as there’s only 1/2 needed values.

The below dev setup is related to the attempted second solution. Originally I tried to pass just certain values from $product or the JSON files but not sure if the formatting of my JSON file is causing the issue with this.

  1. Pass the .site.params needed to populate the SEO partial along with $products.
    In dev setup. Due to the below dev setup causing issues with site.params not working within the SEO partial.

  2. MD page has custom layout

    +++
    title = “Card”
    type = “products”
    layout = “tttoolkit”
    images = [ “https://domain.ard_new_.jpg” ]
    +++

  3. Layout sets $product variable which is the JSON product details & adds in header partial . This is then ranges the product data variable ($product) to “product-button.html” which populates the JSON product data within a button (all working). I then also range that same variable ($product) to the SEO partial which populates the price (.price) stored within in $product.

    {{ $product := where .Site.Data.products.cards “id” “card-12” }}
    {{ partial “header-product.html” . }}
    {{ range $product }}{{ partial “seo_product” . }}{{ end }}

    <>
    {{ range $product }} {{ partial "product-button.html" . }} {{ end }} {{ .Content }}

    Issue: With the above method, now i’m using the range $products, all of the .site params don’t work within the “seo_product” partial.

    Other code:

    SEO Partial (seo_product)

    <script type="application/ld+json">
    {
        "@context" : "http://schema.org",
        "@type" : "Product",
        "mainEntityOfPage": {
             "@id": "{{ .Permalink }}"
        },
        "image": {
          "@type": "ImageObject",
          "url": "{{ if isset .Params "images" }}{{ range .Params.images }}{{ . }}{{ end }}{{ else}}{{ .Site.Params.defaultimage }}{{ end }}"
        },
        "name" : "{{ .Title }}",
        "description" : "{{ if .Description }}{{ .Description }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ end }}{{ end }}",
        "url" : "/{{ ..Permalinkl }}",
        "brand" : "Brand",
        "sku" : "",
        "offers": {
        "@type": "Offer",
          "availability": "http://schema.org/InStock",
          "price": "{{ .price}}",
          "priceCurrency": "GBP"
       }
    }
    </script>
    

    JSON data file
    Contains multiple products within the following syntax

    [{
        "id": "product-id",
        "name": "Card",
        "price": "17.99",
        "weight": "17.99",
        "image": "/images/kit-tools/card.jpeg",
        "description": "Card",
        "url": "/shop-cards/",
        "producturl": "/shop-cards/card",
        "custopt":"0",
        "shippable": "true"
    },        (*This then repeats per product*)
    

    Anyone able to help me with this ? The first solution being would be the most ideal.

Solution 1. original idea was something like this

{{ partial "seo_product.html" ( dict "price" .Site.Data.products.kittools "price" $price ) }}

however I would need to apply
where .Site.Data.products.kittools "id" "acr122u"

As shown in

{{ $product := where .Site.Data.products.kittools "id" "acr122u" }}

This then selects the .price from within the JSON product data

[{
    "id": "product-id",
    "name": "Card",
    "price": "17.99",
    "weight": "17.99",
    "image": "/images/kit-tools/card.jpeg",
    "description": "Card",
    "url": "/shop-cards/",
    "producturl": "/shop-cards/card",
    "custopt":"0",
    "shippable": "true"
},        (*This then repeats per product*)

Would I need to change the JSON formatting to something like this to make it work ?

[
{
"product-name": {
    "id": "product-name",
    "name": "Card",
    "price": "17.99",
    "weight": "17.99",
    "image": "/images/kit-tools/card.jpeg",
    "description": "1 x Card",
    "url": "/shop-kit-tools/",
    "producturl": "/kit/card/",
    "custopt":"0",
    "shippable": "true"
    }
},

I’ve managed to get this mostly working however I’ve got one last issue.

To resolve the SEO .site param issues I’ve removed the partial and added the code in within the product.html template page. However I can’t range over the $product from the same JSON file that the price is being retrieved from.

I’ve got the following setup which works just not with the same JSON file that the product ranges. The formatting for the “testproduct2” has to be within [] where as to call directly from “Site.Data.test.testproduct1.Price” it has to be just {}.

{{ partial "header-product" .}}
{{ $price := .Site.Data.test.testproduct1.Price }}
{{ $product := resources.Get "test/testproduct2.json" | unmarshal}}

$product is then later ranged

{{ range $product }} {{ partial “menu-kit-tools.html” . }} {{ end }}

Results in this error

at <.Site.Data.test.testproduct2.Price>: can't evaluate field Price in type interface {}

JSON Files

“testproduct.json”

{
    "id": "test1",
    "name": "Test 1- Card",
    "price": "40.00",
    "pricepu": "40.00",
    "weight": "40",
    "image": "/images/kit-tools/card.JPG",
    "description": "card",
    "url": "/shop-kit-tools/",
    "producturl": "/kit/card/",
    "custom1name":"Accessories",
    "custom1required":"false",
    "custom1options": "",
    "custopt":"0",
    "shippable": "true"
}

“testproduct2.json”

[{
    "id": "test1",
    "name": "Test 1- Card",
    "price": "40.00",
    "pricepu": "40.00",
    "weight": "40",
    "image": "/images/kit-tools/card.JPG",
    "description": "card",
    "url": "/shop-kit-tools/",
    "producturl": "/kit/card/",
    "custom1name":"Accessories",
    "custom1required":"false",
    "custom1options": "",
    "custopt":"0",
    "shippable": "true"
}]

Solutions !

{{ partial “header-product” .}}
{{ $price := (index .Site.Data.test.testproduct2 0).price }}
{{ $product := .Site.Data.test.testproduct2 }}