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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

TNT Search add custom Snipcart field

plugin plugins theme

Solved by chraebsli View solution

Started by chraebsli 9 months ago · 1 replies · 62 views
9 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
9 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 Rene, 1 week ago
2 41 1 week ago
Plugins · by Xavier, 4 weeks ago
2 50 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1178 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 45 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 68 2 months ago