Merx

Cart

Cart is an extension of ProductList.

Collection of product items.

$item = cart()->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

Cart::__construct()

$cart = new Cart(array $data = []);

There is a cart() shortcut.

Parameters

Name Type Description
data array List of product items. Product items must contain id. quantity, title, price, tax are optional.

$data keys

$data must have an id which has to be a valid product page id. Everything else is taken from the product page. If you set a key the data taken from the product page will be overwritten.

key type description
id string valid product page id
quantity float default is 1
title string taken from product page
price float taken from product page
tax float calculated with product page data

Example

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

Source Code

$cart->add()

Adds item to cart.

$cart->add(array $cartItem): self

Parameters

Name Type Description
cartItem array Must contain a valid product page slug as id.

Return Type

Wagnerwagner\Merx\Cart

Source Code

$cart->update()

Updates existing items.

$cart->update(array $cartItems): parent

Parameters

Name Type Description
cartItems array List of cart items.

Return Type

Wagnerwagner\Merx\ProductList

Source Code

$cart->updateItem()

Updates existing item.

$cart->updateItem(array $item): parent

Parameters

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

Return Type

Wagnerwagner\Merx\ProductList

Source Code

$cart->delete()

Removes Cart from user’s session.

$cart->delete(): void

Return Type

void

Source Code

$cart->remove()

Removes item from Cart by key

$cart->remove($key)

Parameters

Name Type Description
key mixed the name of the key

Source Code

$cart->getTax()

Tax of all items.

$cart->getTax(): float

Return Type

float

Inherits from

Wagnerwagner\Merx\ProductList

Source Code

$cart->getSum()

Sum of all items including tax.

$cart->getSum(): float

Return Type

float

Inherits from

Wagnerwagner\Merx\ProductList

Source Code

$cart->getFormattedItems()

Formats price, tax and sum.

$cart->getFormattedItems(): array

Return Type

array

Inherits from

Wagnerwagner\Merx\ProductList

Source Code

$cart->chunk()

Creates chunks of the same size.

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

Parameters

Name Type Description
size int Number of elements per chunk

Return Type

Wagnerwagner\Merx\Cart

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

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->data()

Getter and setter for the data

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

Parameters

Name Type Description
data array

Return Type

array|\Collection

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->filter()

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

$cart->filter($filter): self

Parameters

Name Type Description
filter \Closure

Return Type

Wagnerwagner\Merx\Cart

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->filterBy()

Filters elements by one of the
predefined filter methods.

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

Parameters

Name Type Description
field string

Return Type

Wagnerwagner\Merx\Cart

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->find()

Find one or multiple elements by id

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

Parameters

Name Type Description
keys string

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->findBy()

Find a single element by an attribute and its value

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

Parameters

Name Type Description
attribute string
value mixed

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->findByKey()

Find a single element by key (id)

$cart->findByKey($key)

Parameters

Name Type Description
key string

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->flip()

Returns the elements in reverse order

$cart->flip(): self

Return Type

Wagnerwagner\Merx\Cart

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->get()

Getter

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

Parameters

Name Type Description
key mixed
default mixed

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->group()

Groups the elements by a given callback

$cart->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

$cart->groupBy()

Groups the elements by a given field

$cart->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

$cart->isEmpty()

Checks if the number of elements is zero

$cart->isEmpty(): bool

Return Type

bool

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->isEven()

Checks if the number of elements is even

$cart->isEven(): bool

Return Type

bool

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->isNotEmpty()

Checks if the number of elements is more than zero

$cart->isNotEmpty(): bool

Return Type

bool

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->isOdd()

Checks if the number of elements is odd

$cart->isOdd(): bool

Return Type

bool

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->last()

Returns the last element

$cart->last()

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->limit()

Returns a new object with a limited number of elements

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

Parameters

Name Type Description
limit int The number of elements to return

Return Type

Wagnerwagner\Merx\Cart

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->map()

Map a function to each element

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

Parameters

Name Type Description
callback callable

Return Type

Wagnerwagner\Merx\Cart

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->nth()

Returns the nth element from the collection

$cart->nth(int $n)

Parameters

Name Type Description
n int

Return Type

mixed

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->not()

Returns a Collection without the given element(s)

$cart->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

$cart->offset()

Returns a new object starting from the given offset

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

Parameters

Name Type Description
offset int The index to start from

Return Type

Wagnerwagner\Merx\Cart

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->paginate()

Add pagination

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

Return Type

\Collection

a sliced set of data

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->pagination()

Get the previously added pagination object

$cart->pagination()

Return Type

\Pagination|null

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->pluck()

Extracts all values for a single field into
a new array

$cart->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

$cart->shuffle()

Shuffle all elements

$cart->shuffle(): self

Return Type

Wagnerwagner\Merx\Cart

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->slice()

Returns a slice of the object

$cart->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\Cart

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->toArray()

Converts the object into an array

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

Return Type

array

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->toJson()

Converts the object into a JSON string

$cart->toJson(): string

Return Type

string

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->without()

Alias for $this->not()

$cart->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