Skip to content
Grav 2.0 is officially stable. Read the announcement →
Plugins

TNT Search add custom Snipcart field

plugin plugins theme

Solved by chraebsli View solution

Started by chraebsli 10 months ago · 1 replies · 210 views
10 months 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
10 months 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 Slebeig, 5 days ago
1 102 5 days ago
Plugins · by Rene, 4 weeks ago
2 302 4 weeks ago
Plugins · by Xavier, 2 months ago
2 222 1 month ago
Plugins · by Luka Prinčič, 7 years ago
3 1410 2 months ago
Plugins · by Sebastian van de Meer, 2 months ago
1 322 2 months ago