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

Cart::__construct()

Constructor

$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(...$args): self

Parameters

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

Return Type

Wagnerwagner\Merx\Cart

Example

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

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
item array Must contain a valid product page id.

Return Type

Wagnerwagner\Merx\ProductList

Source Code

$cart->getStripePaymentIntent()

Get Stripe’s PaymentIntent.

$cart->getStripePaymentIntent(?array $params = [], $options = []): object

Parameters

Name Type Description
params null|array Additional parameters used by \Stripe\PaymentIntent::create().
options null|array|\Stripe\Util\RequestOptions Additional options used by \Stripe\PaymentIntent::create().

Return Type

object

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

Return Type

$this

Source Code

$cart->getTax()

Tax of all items.

$cart->getTax(): float

Return Type

float

Inherits from

Wagnerwagner\Merx\ProductList

Source Code

$cart->getTaxRates()

Taxes grouped by tax rate

$cart->getTaxRates(): array

Return Type

array

Inherits from

Wagnerwagner\Merx\ProductList

Example

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

dump($cart->getTaxRates());

Returns

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

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->payPalPurchaseUnits()

Returns an array in the format of PayPal’s purchase_unit_request object.

$cart->payPalPurchaseUnits(): array

If you use this method in combination with the paypal.purchaseUnits option you can provide additional information of the user’s cart to PayPal.

Return Type

array

Example

$cart = cart([
  [
    'id' => 'apple',
    'title' => 'Apple',
    'quantity' => 6,
    'price' => 0.99,
  ],
  [
    'id' => 't-shirt',
    'title' => 'T-Shirt',
    'quantity' => 2,
    'price' => 49.99,
  ]
]);

dump($cart->toPayPalPurchaseUnits());

Returns

Array
(
  [0] => Array
    (
      [description] => My Online Shop
      [amount] => Array
        (
          [value] => 105.92
          [currency_code] => EUR
          [breakdown] => Array
            (
              [item_total] => Array
                (
                  [value] => 105.92
                  [currency_code] => EUR
                )
            )
        )
      [items] => Array
        (
          [0] => Array
            (
              [name] => Apple
              [unit_amount] => Array
                (
                  [value] => 0.99
                  [currency_code] => EUR
                )
              [quantity] => 6
            )
          [1] => Array
            (
              [name] => T-Shirt
              [unit_amount] => Array
                (
                  [value] => 49.99
                  [currency_code] => EUR
                )
              [quantity] => 2
            )
        )
    )
)

Source Code

$cart->chunk()

Creates chunks of the same size.

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

$cart->count()

Counts all elements

$cart->count(): int

Return Type

int

Inherits from

Kirby\Toolkit\Iterator

Source Code

$cart->data()

Getter and setter for the data

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

Parameters

Name Type Description
data array|null

Return Type

array|$this

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->empty()

Clone and remove all elements from the collection

$cart->empty()

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->filter()

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

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

Parameters

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

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->filterBy()

Alias for Kirby\Toolkit\Collection::filter

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

Parameters

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

Return Type

static

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

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->findByKey()

Find a single element by key (id)

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

Return Type

static

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

$cart->group($field, bool $i = true)

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 subcollection in
each group

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->groupBy()

Alias for Kirby\Toolkit\Collection::group

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

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

Parameters

Name Type Description
limit int The number of elements to return

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->map()

Map a function to each element

$cart->map(callable $callback)

Parameters

Name Type Description
callback callable

Return Type

$this

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

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->offset()

Returns a new object starting from the given offset

$cart->offset(int $offset)

Parameters

Name Type Description
offset int The index to start from

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->paginate()

Add pagination

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

Parameters

Name Type Description
arguments array

Return Type

static

a sliced set of data

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->pagination()

Get the previously added pagination object

$cart->pagination()

Return Type

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

Return Type

array

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->shuffle()

Shuffle all elements

$cart->shuffle()

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code

$cart->slice()

Returns a slice of the object

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

$cart->toArray()

Converts the object into an array

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

Parameters

Name Type Description
map \Closure|null

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

Return Type

static

Inherits from

Kirby\Toolkit\Collection

Source Code