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['taxRate']; 19
$item['quantity']; // 2
$item['sum']; // 199.98
$item['sumTax']; // 31.9295798319

ProductList::__construct()

Constructor

$productList = new ProductList(array $data = [], bool $caseSensitive = false);

Parameters

Name Type Description
data array
caseSensitive bool Whether the collection keys should be
treated as case-sensitive

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)

Parameters

Name Type Description
args mixed ($data) or ($id, $data).

Return Type

$this

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

Return Type

$this

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->getTax()

Tax of all items.

$productList->getTax(): float

Return Type

float

Source Code

$productList->getTaxRates()

Taxes grouped by tax rate

$cart->getTaxRates(): array

Return Type

array

Inherits from

Wagnerwagner\Merx\ProductList

Example

use Wagnerwagner\Merx\ProductList;

$productList = new ProductList([
  [
    'id' => 'apple',
    'quantity' => 6,
    'price' => 0.99,
    'taxRate' => 7, // in percent
  ],
  [
    'id' => 't-shirt',
    'quantity' => 2,
    'price' => 49.99,
    'taxRate' => 19, // in percent
  ]
]);

dump($productList->getTaxRates());

Returns

Array
(
  [0] => Array
    (
      [taxRate] => 7
      [sum] => 0.38859813084112
    )
  [1] => Array
    (
      [taxRate] => 19
      [sum] => 15.963193277311
    )
)

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)

Parameters

Name Type Description
size int Number of elements per chunk

Return Type

static

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

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->count()

Counts all elements

$cart->count(): int

Return Type

int

Inherits from

Kirby\Toolkit\Iterator

Source Code

$productList->data()

Getter and setter for the data

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

Parameters

Name Type Description
data array|null

Return Type

array|$this

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->empty()

Clone and remove all elements from the collection

$productList->empty()

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->filter()

Filters elements by one of the
predefined filter methods, by a
custom filter function or an array of filters

$productList->filter($field, ...$args)

Parameters

Name Type Description
field string|array|\Closure
args mixed

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->filterBy()

Alias for Kirby\Toolkit\Collection::filter

$productList->filterBy(...$args)

Parameters

Name Type Description
field string|array|\Closure
args mixed

Return Type

static

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|null

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->findByKey()

Find a single element by key (id)

$productList->findByKey(string $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()

Return Type

static

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 field or callback function

$productList->group($field, bool $caseInsensitive = true)

Parameters

Name Type Description
field string|\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()

Alias for Kirby\Toolkit\Collection::group

$productList->groupBy(...$args)

Parameters

Name Type Description
field string|\Closure
i bool

Return Type

\Kirby\Toolkit\Collection

A new collection with an element for
each group and a sub collection 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)

Parameters

Name Type Description
limit int The number of elements to return

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->map()

Map a function to each element

$productList->map(callable $callback)

Parameters

Name Type Description
callback callable

Return Type

$this

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
keys string any number of keys, passed as individual arguments

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->offset()

Returns a new object starting from the given offset

$productList->offset(int $offset)

Parameters

Name Type Description
offset int The index to start from

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->paginate()

Add pagination

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

Parameters

Name Type Description
arguments array

Return Type

$this|static

a sliced set of data

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->pagination()

Get the previously added pagination object

$productList->pagination()

Return Type

\Kirby\Toolkit\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|null
unique bool

Return Type

array

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->shuffle()

Shuffle all elements

$productList->shuffle()

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->slice()

Returns a slice of the object

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

Parameters

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

Return Type

$this|static

Inherits from

Kirby\Toolkit\Collection

Source Code

$productList->toArray()

Converts the object into an array

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

Parameters

Name Type Description
map \Closure|null

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
keys string any number of keys, passed as individual arguments

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code