Does current page exist elsewhere in a Structure field?

I have a list of products that are logged in a page field inside a structure field on the user’s account when purchased. I’m try to do a check on the product page to see if the product exists in the structure field of the user’s content file like this:

    if ($kirby->user()->products()->toStrucure()->findBy('product', $page)) {
        $purchased  = true;
    } else {
        $purchased  = false;

This doesn’t work though, $purchased is true regardless.

I’m echoing the values like this check if they should be matching or not:

$purchases = $kirby->user()->products()->toStructure();
foreach($purchases as $purchase){
    echo $purchase->product()->toPage();
echo $page;

It and looks as though it should work as I intend. Where am I going wrong here?

You cannot use findBy here. $page is a page object, but product is not.

$purchased = $kirby->user()->products()->toStrucure()->filter(function($item) use($page) {
  return $item->product()->toPage() === $page;
1 Like

I thought it might be something like that (using toPage()) but couldn’t quite work it out. Thank you!

What’s the best way to access the found items? ->isNotEmpty() is returning a bool, if I remove that, $purchased is a structure object with integers for where the found items were in the structure I filtered.


$kirby->user()->products()->toStrucure()->filter(function($item) use($page) {
  return $item->product()->toPage() === $page;

return a normal structure object, you can loop through the items as usual.

If you only want the first item, chain first() onto it and then access the fields:

if ($item = $kirby->user()->products()->toStrucure()->filter(function($item) use($page) {
  return $item->product()->toPage() === $page;
})->first()) {
  echo $item->whateverfieldthereis();
1 Like

Ah yes, I was completely overcomplicating this. Thank you!

I have a setup where a product ($page) can exist elsewhere in a pages field as part of a bundle. I’m trying to check if the user owns the bundle that contains the product, but as the user can own multiple bundles that contain the product I’d like to get them all, sort by date, and select the most recent:

//Get All Bundles
$allBundles = page('products')->children()->template('bundle');
//Check which Bundles Product exists in
$inBundles = $allBundles->filter(function($bundle) use($page) {
    return $bundle->products()->toPages()->has($page);
//Check if user owns Bundle <----- The bit I can't work out
foreach($inBundles as $bundle){
    $purchased = $kirby->user()->products()->toStructure()->filter(function($item) use($bundle) {
        return $item->product()->toPage() === $bundle;

$purchased here just keeps getting overwritten. How can I add items to $purchased for filtering later?

Not sure what the expected result is? A set of pages? A set of structure items?

As Structure with items. At the moment $purchased is a structure with a single item that is being returned multiple times because of the foreach (when $inBundles has multiple items)

Then you would have to collect the items into a new Structure object.

$purchased = new Structure();
// then add elements to structure in loop

Of course, thanks!