Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

TNT Search add custom Snipcart field

plugin plugins theme

Solved by chraebsli View solution

Started by chraebsli 1 year ago · 1 replies · 778 views
1 year ago

I am creating a shop for a customer with the Snipcart plugin. There will be a TNT search on the site, using the tntsearch plugin. I checked the docs how to add a custom field, and therefore I added this function to the theme.php:

PHP
// custom TNT Search indexing to add ID field
public function onTNTSearchIndex(Event $e) {
    $fields = $e['fields'];
    $page = $e['page'];

    if (isset($page->header()->snipcart->product_id)) {
        $fields->product_id = $page->header()->snipcart->product_id;
    }
}

According to docs, this should create a custom field product_id with the content of page->header()->snipcart->product_id, but it doesn’t.

Frontmatter of a page looks like this:

YAML
title: Product Name
snipcart:
  product_id: 1005
  price: 1990.0

Debug

I debugged and here’s what I got:

PHP
$this->grav[‘log’]->info(‘Header’, (array)$page->header()->snipcart);

// Output: Here I get the Snipcart object as it should be
Header {"product_id":4012,"price":2990} []
PHP
$this->grav[‘log’]->info(‘Product ID’, (array)$page->header()->snipcart->product_id);

// Output: Here I tried to access the object I got from the above output and get nothing
Product ID [] []
last edited 09/16/25 by chraebsli
1 year ago Solution

Ahh nvm I got it. I accessed it the wrong way. Instead of $page->header()->snipcart->product_id it should be $page->header()->snipcart['product_id'].

Final PHP:

PHP
// custom TNT Search indexing to add ID field
public function onTNTSearchIndex(Event $e) {
    $fields = $e['fields'];
    $page = $e['page'];

    if (isset($page->header()->snipcart['product_id'])) {
        $fields->product_id = $page->header()->snipcart['product_id'];
    }
}
👍 1

Suggested topics

Topic Participants Replies Views Activity
Plugins · by DrPen, 3 weeks ago
2 285 3 weeks ago
Plugins · by renedebaaij, 4 weeks ago
1 262 3 weeks ago
Plugins · by Thomas, 4 weeks ago
1 275 4 weeks ago
Plugins · by complanar, 2 months ago
6 596 1 month ago
Plugins · by BenLaKnet, 1 month ago
1 499 1 month ago