Fetch structure field entry by clicking button?

Hey guys,
I got a question once again.

I’ve got a list of products on my site and each product has a button on it’s card which says “view product”.
Each product is saved in a structure field with fields like “Name”, “Product Image”, “Price” and so on.

I’m trying to have a single template site called single-product.php, where all the structure field entries get fetched by clicking on the “view product” button and displayed on a single page.

Is this possible? Do I need some kind of plugin?

Best regards,

All of this is possible - out of the box - if you use a page for each product instead of putting them all in a structure. Why would you do that?

1 Like

I agree with @bvdputte, why don’t you store your products in a separate page?

However, it is possible to do what you want with a route that fetches the product information from the structure field and then passes it to the template, together with all the other data that is needed in the snippets (Kirby object, Site object, etc.). The problem is that you won’t have but need a page object and have to either use the parent object as a basis or create a single subpage within the products folder.

You’d also have to urlencode() and urldecode() the field of your structure field that you want to use in the URL.

In conclusion, while using routes to create virtual pages is possible, it has some downsides due to the fact that Kirby pages are tied to folders.

If you have already stored a lot of products in structure fields and want to go down the subpage route, you can create these pages programmatically from the structure fields, using Kirby’s API.

1 Like

At the end it will be about 400 to 500 products, thats why I thought it might be faster by using a structure field.
I guess I will try out to store them in a site, since that’s much easier.
Thanks :slight_smile:

What do you mean with faster, creating a new product?

I guess it’s a little bit faster adding a structure field item, than a new subpage, isn’t it? :thinking:

Yes, but to create sibling pages faster, you might consider using a plugin, like clonr

1 Like

That sounds pretty good and I got an idea how to use it already. Thanks for showing me this :+1:
Is it only possible to have a list of products in the subpages menu or can i create a more detailed list in the panel, like it would be with a structure field?

You mean in the sidebar?

Yes, when I create a product via subpage it just shows the name on the sidebar, but is it possible to create a products-list in the main page in the panel?
I hope I explained it well with my english, else I try it in german :sweat_smile:

You could create a widget for the Panel Dashboard, for example (use the Panel pages widget as a basis and extend as required). Another option would be something like the subpage list field: https://github.com/flokosiol/kirby-subpagelist. As a field, you would put this in the parent page.

It doesn’t show much more information, but you could extend it to also show basic product information.

1 Like

I got a problem now I’ve never encountered before.
This is my code now.

  <?php foreach($page->children() as $product): ?>

    <div class="p-2 item
    <?= str_replace(',', ' ', $product->schulklasse()) ?>
    <?= str_replace(',', ' ', $product->form()) ?>
    <?= str_replace(',', ' ', $product->geschlecht()) ?>
    <?= str_replace(',', ' ', $product->groesse()) ?>
    <?= str_replace(',', ' ', $product->marke()) ?>


        <div class="card" style="width: 18rem;">
          <?php snippet('coverimage', $product) ?>
          <div class="card-body">
            <h2 class="card-title text-center"><?= $product->name()->text() ?></h2>
            <p class="card-text"><?= $product->desc()->text()->kirbytext()->excerpt(60, 'chars') ?></p>
            <p class="card-text"> <small>Eigengewicht: <?= $product->gewicht()->text()?>g</p></small>
            <div class="text-center">
              <a href="<?= $product->url() ?>"><button class="btn btn-primary btn-produkt" name="button"> Zum Produkt </button></a>

    <hr />

  <?php endforeach ?>

When I want to save the page I get the following error:

Too few arguments to function Kirby\Panel\Models\Page::form(), 0 passed in C:\xampp\htdocs\schulranzen\panel\app\src\panel\models\page\changes.php on line 71 and exactly 2 expected in file: C:\xampp\htdocs\schulranzen\panel\app\src\panel\models\page.php on line: 144