TNT Search add custom Snipcart field

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:

// 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:

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

Debug

I debugged and here’s what I got:

$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} []
$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 [] []

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:

// 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 Like