I’ve got an array I return as a Kirby\Toolkit\Collection
. I’d like to map a function to each element.
As it is now, it returns an array and when I use it in a loop it’s mixing $cartItem['product_uri']
with $product->title()
making it look confusing to use. I’d prefer $cartItem->product_uri()
with $product->title()
I’ve got it to somewhat work. In the code provided below, it’s looping two items. But e.g. the $cartItem->product_uri()
only return a value from the last item in the cart. What am I missing?
The array in the class ($this->items
):
[
{
"product_uri": "path/to/product1",
"product_uuid": "CTDBbFE1AOyrgIzQ",
},
{
"product_uri": "path/to/product2",
"product_uuid": "Vj8W5NjZbf9bEVBu",
}
]
And in the class:
public function getProducts()
{
$collection = new Collection( $this->items ); // Using the above array
$collection->map( function( $collection ) {
$this->product_uri = $collection[ 'product_uri' ];
$this->product_uuid = $collection[ 'product_uuid' ];
return $this;
});
return $collection;
}