PHPackages                             lenius/basket - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lenius/basket

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lenius/basket
=============

Shopping basket package

v5.0.1(2y ago)1327.7k1[4 PRs](https://github.com/Lenius/basket/pulls)3MITPHPPHP ^8.0CI passing

Since Aug 14Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/Lenius/basket)[ Packagist](https://packagist.org/packages/lenius/basket)[ Docs](https://github.com/lenius/basket)[ RSS](/packages/lenius-basket/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (23)Used By (3)

Shopping Basket Package
=======================

[](#shopping-basket-package)

[![Latest Stable Version](https://camo.githubusercontent.com/ed112a0e7c2514de588b1c058fbd9fb9bc71c52868b7f850cbc91ca7cb21c0c4/68747470733a2f2f706f7365722e707567782e6f72672f4c656e6975732f6261736b65742f762f737461626c652e737667)](https://packagist.org/packages/Lenius/basket)[![Basket](https://github.com/Lenius/basket/actions/workflows/php.yml/badge.svg)](https://github.com/Lenius/basket/actions/workflows/php.yml)[![Code Coverage](https://camo.githubusercontent.com/7afadc817bf0bc58121ae328f2aafe08a107f72564565457ca975322529abea7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4c656e6975732f6261736b65742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Lenius/basket/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/1058d7eb7aa5ba2e16f341a432147f3c46b226e00156ba0fd9d0c1e8aba7b99f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4c656e6975732f6261736b65742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Lenius/basket/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/9ad99c972f11d5f59ddaa8c2577f996e08fa9df5183622d7d857e388f8ccf581/68747470733a2f2f706f7365722e707567782e6f72672f4c656e6975732f6261736b65742f646f776e6c6f6164732e737667)](https://packagist.org/packages/Lenius/basket)[![License](https://camo.githubusercontent.com/0d1e37f41c757685b390ea450d2fa92fe065b41c58460e5b58de801c1dda42cd/68747470733a2f2f706f7365722e707567782e6f72672f4c656e6975732f6261736b65742f6c6963656e73652e737667)](https://packagist.org/packages/Lenius/basket)

The Lenius shopping basket composer package makes it easy to implement a shopping basket into your application and store the basket data using one of the numerous data stores provided. You can also inject your own data store if you would like your basket data to be stored elsewhere.

Installation
------------

[](#installation)

Using [composer](https://packagist.org/packages/lenius/basket):

```
$ composer require lenius/basket:^4.0 (PHP7.4)
$ composer require lenius/basket:^5.0 (PHP8.x)
```

Usage
-----

[](#usage)

Below is a basic usage guide for this package.

### Instantiating the basket

[](#instantiating-the-basket)

Before you begin, you will need to know which storage and identifier method you are going to use. The identifier is how you store which basket is for that user. So if you store your basket in the database, then you need a cookie (or some other way of storing an identifier) so we can link the user to a stored basket.

In this example we're going to use the cookie identifier and session for storage.

```
use Lenius\Basket\Basket;
use Lenius\Basket\Storage\Session;
use Lenius\Basket\Identifier\Cookie;

$basket = new Basket(new Session, new Cookie);
```

### Inserting items into the basket

[](#inserting-items-into-the-basket)

Inserting an item into the basket is easy. The required keys are id, name, price, weight and quantity, although you can pass over any custom data that you like.

```
$basket->insert(new Item([
    'id'       => 'foo',
    'name'     => 'bar',
    'price'    => 100,
    'quantity' => 2,
    'weight'   => 300
]));
```

### Inserting items with options into the basket

[](#inserting-items-with-options-into-the-basket)

Inserting an item into the basket is easy. The required keys are id, name, price and quantity, although you can pass over any custom data that you like. If option items contains price or weight there values are added to the total weight / price of the product.

```
$basket->insert(new Item([
    'id'         => 'foo',
    'name'       => 'bar',
    'price'      => 100,
    'quantity'   => 2,
    'weight'     => 300,
    'options'    => [
       [
        'name'   => 'Size',
        'value'  => 'L',
        'weight' => 50,
        'price'  => 10
       ],
     ],
]));
```

### Setting the tax rate for an item

[](#setting-the-tax-rate-for-an-item)

Another key you can pass to your insert method is 'tax'. This is a percentage which you would like to be added onto the price of the item.

In the below example we will use 25% for the tax rate.

```
$basket->insert(new Item([
    'id'       => 'mouseid',
    'name'     => 'Mouse',
    'price'    => 100,
    'quantity' => 1,
    'tax'      => 25,
    'weight'   => 200
]));
```

### Updating items in the basket

[](#updating-items-in-the-basket)

You can update items in your basket by updating any property on a basket item. For example, if you were within a basket loop then you can update a specific item using the below example.

```
foreach ($basket->contents() as $item) {
    $item->name = 'Foo';
    $item->quantity = 1;
}
```

### Destroying/emptying the basket

[](#destroyingemptying-the-basket)

You can completely empty/destroy the basket by using the `destroy()` method.

```
$basket->destroy()
```

### Retrieve the basket contents

[](#retrieve-the-basket-contents)

You can loop the basket contents by using the following method

```
$basket->contents();
```

You can also return the Basket items as an array by passing true as the first argument

```
$basket->contents(true);
```

### Retrieving the total items in the Basket

[](#retrieving-the-total-items-in-the-basket)

```
$basket->totalItems();
```

### Retrieving the Basket total

[](#retrieving-the-basket-total)

```
$basket->total();
```

By default the `total()` method will return the total value of the Basket as a `float`, this will include any item taxes. If you want to retrieve the Basket total without tax then you can do so by passing false to the `total()` method

```
$basket->total(false);
```

### Check if the Basket has an item

[](#check-if-the-basket-has-an-item)

```
$basket->has($itemIdentifier);
```

### Remove an item from the Basket

[](#remove-an-item-from-the-basket)

```
$basket->remove($identifier)
```

### Retrieve an item object by identifier

[](#retrieve-an-item-object-by-identifier)

```
$basket->item($itemIdentifier);
```

Basket items
------------

[](#basket-items)

There are several features of the Basket items that may also help when integrating your Basket.

### Check if an item has options

[](#check-if-an-item-has-options)

You can check if a Basket item has options by using the `hasOptions()` method.

```
foreach ($basket->contents() as $item) {
    if ($item->hasOptions()) {
        // We have options
    }
}
```

### You can also get the total weight for a single item

[](#you-can-also-get-the-total-weight-for-a-single-item)

```
$item->weight();
```

### Output the item data as an array

[](#output-the-item-data-as-an-array)

```
$item->toArray();
```

Testing
-------

[](#testing)

Run the tests with:

```
composer psalm
composer stan
composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security-related issues, please email instead of using the issue tracker.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance54

Moderate activity, may be stable

Popularity35

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 97.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~204 days

Recently: every ~234 days

Total

17

Last Release

1022d ago

Major Versions

v1.0.6 → v2.0.02017-10-11

v2.0.2 → v3.0.02019-01-25

v3.0.1 → v4.0.02021-01-06

v4.0.2 → v5.0.02022-05-25

PHP version history (6 changes)v1.0.0PHP &gt;=5.4.0

v1.0.1PHP &gt;=5.5.5

v1.0.2PHP &gt;=5.6.0

v1.0.3PHP &gt;=7.0.0

v4.0.0PHP ^7.4|^8.0

v5.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/8539c9af6113bdef769983294e463381bace6566ecc7c8daaa7dcfe1516c19de?d=identicon)[lenius](/maintainers/lenius)

---

Top Contributors

[![cjonstrup](https://avatars.githubusercontent.com/u/576584?v=4)](https://github.com/cjonstrup "cjonstrup (171 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (3 commits)")[![renovate-bot](https://avatars.githubusercontent.com/u/25180681?v=4)](https://github.com/renovate-bot "renovate-bot (2 commits)")

---

Tags

baskete-commercephpshopping-baskete-commerceshopping

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/lenius-basket/health.svg)

```
[![Health](https://phpackages.com/badges/lenius-basket/health.svg)](https://phpackages.com/packages/lenius-basket)
```

###  Alternatives

[sylius/refund-plugin

Plugin provides basic refunds functionality for Sylius application.

691.7M14](/packages/sylius-refund-plugin)[syscover/shopping-cart

Shopping Cart package

299.1k1](/packages/syscover-shopping-cart)[pastuhov/yii2-yml-catalog

YML (Yandex Market Language) generator.

2116.7k](/packages/pastuhov-yii2-yml-catalog)[whitecube/laravel-prices

Manage acquisition, selling &amp; renting prices for products and services in your Laravel Application

121.6k](/packages/whitecube-laravel-prices)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
