Merx

ProductList

The ProductList class extends the Kirby\Toolkit\Collection.

What is special about that, is that you can pass just the id of a product page and the price and tax are automatically calculated for you. See the Wishlist cookbook to learn more.

Collection of product items.

$item = $productList->first();
$item['id']; // nice-shoes
$item['title']; // Nice Shoes
$item['price']; // 99.99
$item['tax']; // 15.964789915966
$item['quantity']; // 2
$item['sum']; // 199.98
$item['sumTax']; // 31.9295798319

ProductList::__construct()

Constructor

$productList = new ProductList(array $data = []);

Parameters

Name Type Description
data array

Inherits from

Kirby\Toolkit\Collection

Example

$productList = new ProductList([
  [
    'id' => 'new-shoes',
    'quantity' => 3,
  ],
  [
    'id' => 'blue-socks'
  ],
]);

Source Code

$productList->append()

Appends item to ProductList

$productList->append(...$args): self

Parameters

Name Type Description
args mixed ($data) or ($id, $data). $data must contain a valid product page id.

Return Type

Wagnerwagner\Merx\ProductList

Source Code

$productList->updateItem()

Updates existing ProductList item

$productList->updateItem(array $item): self

Parameters

Name Type Description
item array Must contain a valid product page id.

Return Type

Wagnerwagner\Merx\ProductList

Example


$productList->updateItem([
  'id' => 'nice-shoes',
  'price' => 89.99,
  'tax' => 14.3681512605,
]);

Source Code

$productList->remove()

Removes an element from the array by key

$productList->remove($key)

Parameters

Name Type Description
key mixed the name of the key

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->getTax()

Tax of all items.

$productList->getTax(): float

Return Type

float

Source Code

$productList->getSum()

Sum of all items including tax.

$productList->getSum(): float

Return Type

float

Source Code

$productList->getFormattedItems()

Formats price, tax and sum.

$productList->getFormattedItems(): array

Return Type

array

Source Code

$productList->chunk()

Creates chunks of the same size.

$productList->chunk(int $size): self

Parameters

Name Type Description
size int Number of elements per chunk

Return Type

Wagnerwagner\Merx\ProductList

A new collection with an element for each chunk and
a sub collection in each chunk

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->data()

Getter and setter for the data

$productList->data(array $data = null)

Parameters

Name Type Description
data array

Return Type

array|\Collection

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->filter()

Filters elements by a custom
filter function or an array of filters

$productList->filter($filter): self

Parameters

Name Type Description
filter \Closure

Return Type

Wagnerwagner\Merx\ProductList

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->filterBy()

Filters elements by one of the
predefined filter methods.

$productList->filterBy(string $field, ...$args): self

Parameters

Name Type Description
field string

Return Type

Wagnerwagner\Merx\ProductList

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->find()

Find one or multiple elements by id

$productList->find(...$keys)

Parameters

Name Type Description
keys string

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->findBy()

Find a single element by an attribute and its value

$productList->findBy(string $attribute, $value)

Parameters

Name Type Description
attribute string
value mixed

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->findByKey()

Find a single element by key (id)

$productList->findByKey($key)

Parameters

Name Type Description
key string

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->flip()

Returns the elements in reverse order

$productList->flip(): self

Return Type

Wagnerwagner\Merx\ProductList

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->get()

Getter

$productList->get($key, $default = null)

Parameters

Name Type Description
key mixed
default mixed

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->group()

Groups the elements by a given callback

$productList->group(Closure $callback): Collection

Parameters

Name Type Description
callback \Closure

Return Type

Kirby\Toolkit\Collection

A new collection with an element for each group and a subcollection in each group

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->groupBy()

Groups the elements by a given field

$productList->groupBy(string $field, bool $i = true)

Parameters

Name Type Description
field string
i bool

Return Type

\Collection

A new collection with an element for each group and a subcollection in each group

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->isEmpty()

Checks if the number of elements is zero

$productList->isEmpty(): bool

Return Type

bool

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->isEven()

Checks if the number of elements is even

$productList->isEven(): bool

Return Type

bool

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->isNotEmpty()

Checks if the number of elements is more than zero

$productList->isNotEmpty(): bool

Return Type

bool

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->isOdd()

Checks if the number of elements is odd

$productList->isOdd(): bool

Return Type

bool

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->last()

Returns the last element

$productList->last()

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->limit()

Returns a new object with a limited number of elements

$productList->limit(int $limit): self

Parameters

Name Type Description
limit int The number of elements to return

Return Type

Wagnerwagner\Merx\ProductList

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->map()

Map a function to each element

$productList->map(callable $callback): self

Parameters

Name Type Description
callback callable

Return Type

Wagnerwagner\Merx\ProductList

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->nth()

Returns the nth element from the collection

$productList->nth(int $n)

Parameters

Name Type Description
n int

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->not()

Returns a Collection without the given element(s)

$productList->not(...$keys)

Parameters

Name Type Description
\args any number of keys, passed as individual arguments

Return Type

\Collection

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->offset()

Returns a new object starting from the given offset

$productList->offset(int $offset): self

Parameters

Name Type Description
offset int The index to start from

Return Type

Wagnerwagner\Merx\ProductList

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->paginate()

Add pagination

$productList->paginate(...$arguments)

Return Type

\Collection

a sliced set of data

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->pagination()

Get the previously added pagination object

$productList->pagination()

Return Type

\Pagination|null

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->pluck()

Extracts all values for a single field into
a new array

$productList->pluck(string $field, string $split = null, bool $unique = false): array

Parameters

Name Type Description
field string
split string
unique bool

Return Type

array

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->shuffle()

Shuffle all elements

$productList->shuffle(): self

Return Type

Wagnerwagner\Merx\ProductList

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->slice()

Returns a slice of the object

$productList->slice(int $offset = 0, int $limit = null): self

Parameters

Name Type Description
offset int The optional index to start the slice from
limit int The optional number of elements to return

Return Type

Wagnerwagner\Merx\ProductList

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->toArray()

Converts the object into an array

$productList->toArray(Closure $map = null): array

Return Type

array

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->toJson()

Converts the object into a JSON string

$productList->toJson(): string

Return Type

string

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->without()

Alias for $this->not()

$productList->without(...$keys)

Parameters

Name Type Description
\args any number of keys, passed as individual arguments

Return Type

\Collection

Inherits from

Kirby\Toolkit\Collection

Source Code