Conditional to render products by specific designer

Hi there –

I’m rebuilding my Kirby install to support ecommerce, and am admittedly re-learning PHP. We will be representing products from a growing group of designers and we want to highlight those designers on the site. The current structure looks like:

Designers

  • Designer A
  • Designer B
  • Designer C
  • etc

Products

  • Product A
  • Product B
  • Product C
  • Product D
  • etc

Products could be designed by a single designer or be a collaboration by multiple designers. On each Designer profile I want to render those products they are associated with. I also want to render designer information on the product pages.

My plan has been to do this through the use of tags. Each product would be tagged with the designer name, and the designers profile page would be tagged with their name.

Here are my questions:

  1. Is this possible in Kirby?
  2. Is this the best use of Kirby or is there a more effective way?
  3. Direction of conditional syntax would be hugely appreciated.

I would probably do this the Author way (Author = Designer.) See here. That way you can use tags for product types rather than who designed it, and still be able to pull up a list of products designed by a particular person(s).

You could pump a designer list from the hidden designer folder into a Multiselect field via JSON to have the ability to select multiple desingers on a product.

I would also do this only as a one way relation, assigning designers to products and not the also the other way round.

On each designer page, you can then get the the products designed by the designer with a filter function.

$designerProducts = page('products')->children()->visible()->filterBy('designer', $page->uid()->value(), ',');

On each product page, you can use a multi-select or a relationship field to assign the designers to each product, and fetch the information about the designer on each product page.

You don’t need additional tags in this case.

The logic to fetch the products belonging to each designer is best wrapped into a page model:

Assuming your template is called designer.php, create a model `controllers/designer.php

class DesignerPage extends Page {
  public function getAssociatedProducts($field) {
       $designerProducts = page('products')->children()->visible()->filterBy($field, $this->uid()->value(), ',');
       return $designerProducts;
  }

}

In your template, you can then call it like this:

// pass the field as a parameter
$products = $page->getAssociatedProducts('designer');

To make this more versatile, we could of course also make the page to fetch from a parameter instead of hard-coding it into the function:

class DesignerPage extends Page {
  public function getAssociatedProducts($source, $field) {
   return false;
    if($source = page($sourcePage)) {
      $designerProducts = $source->children()->visible()->filterBy($field, $page->uid()->value(), ',');
       return $designerProducts;
    }     
  }

}

// in template
// pass source and  field as parameters
$products = $page->getAssociatedProducts('products', 'designer');

Texnixe many thanks for this perspective. Is what you’re suggesting a build atop of jimbobrjames suggesting to use the Author feature (designer = author)?

Yes, you would use a multi-select or relationship field in the product page to select the designers. Your folder setup would remain the same as outlined in your first post.

The cookbook recipe is a starting point, but the example described there uses a single select field.

For page models, see here: https://getkirby.com/docs/developer-guide/advanced/models

Additional note: The relationship field is useful if you want to allow the user to manually order the selected designers. That is one big difference between the two fields, apart from their visual differences. The result as stored in the content file is always a comma separated list of values.

1 Like

From a syntax perspective could I instead of creating an Authors directory and using Authors as the keyword, could I use Designer?

I like the Authors feature but would love to distinguish between Authors and Designers. So in effect could I have an Authors directory and a Designers directory effectively duplicating the functionality w/ Authors?

You care absolutely free to call your folder what you want and call the field what you want. If you have designers, call them designers, not authors, makes it a lot easier to understand your code.

product.yml blueprint

fields:
  designers:
    label: Designers
    type: multiselect #or relationship
    query:
       page: designers
       fetch: children
       text: '{{ title }}'
       value: '{{ uri }}'
    help: Choose one or more designers
1 Like