Hello,
While using MERX, in order to return product variants on the fly, I am testing this idea which uses virtual pages:
The idea is to create a page model for the product page, in order to override the children() method, so it returns product variants as a collection of virtual children pages.
In my case, tho, there are some differences.
- The blueprint I want to create a model for is called fanware-product.yml and I don’t have a fanware-product.php template because I don’t visit this page at any time.
- In this case what should drive the creation of virtual children is a sizes() structure field.
So my first attempt for the model is this:
/site/models/fanwareproduct.php
<?php
class FanwareProductPage extends Kirby\Cms\Page
{
public function children()
{
$sizedVariants = [];
foreach ($this->sizes()->toStructure() as $size) {
$sizedVariants[] = [
'slug' => $this->slug() . $size->size(),
'template' => 'product-variant',
'model' => 'product-variant',
'content' => [
'title' => $this->title() . '(' . $size->size() . ')',
'price' => $this->price()->toFloat(),
]
];
}
return Pages::factory($sizedVariants, $this);
}
}
I test this in the homepage (not in the fanware product page) with the following code :
// $fanwareProducts only has 1 page in it
<?php foreach($fanwareProducts->shuffle() as $fanwareProduct): ?><?php dump($fanwareProduct->sizes()) ?>
<?php dump($fanwareProduct->children()) ?>
<?php endforeach ?>
Which results in:
Kirby\Cms\Field Object
(
[sizes] => -
size: S
outofstock: 'false'
default: 'true'
-
size: M
outofstock: 'false'
default: 'false'
-
size: L
outofstock: 'true'
default: 'false'
-
size: XL
outofstock: 'false'
default: 'false'
)
Kirby\Cms\Pages Object
(
)
…so I am getting an empty children collection
Is there anything specifically wrong about this model or how I am using it ?
Thanks