PHPackages                             mrprompt/shopping-cart - 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. mrprompt/shopping-cart

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

mrprompt/shopping-cart
======================

A reusable shopping cart implementation

41PHPCI failing

Since Dec 30Pushed 7y agoCompare

[ Source](https://github.com/mrprompt/ShoppingCart)[ Packagist](https://packagist.org/packages/mrprompt/shopping-cart)[ RSS](/packages/mrprompt-shopping-cart/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (2)Used By (0)

ShoppingCart
============

[](#shoppingcart)

[![Build Status](https://camo.githubusercontent.com/d01a19fc50d149728ec9f5abd790a9877213680f89f9b58c0b9225ca87148e06/68747470733a2f2f7472617669732d63692e6f72672f6d7270726f6d70742f53686f7070696e67436172742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mrprompt/ShoppingCart)[![Build Status](https://camo.githubusercontent.com/3eb92be0f464bf94537615e7299dadb0d00ad43182aeb5d2fb9b9c9c2e53fb69/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d7270726f6d70742f53686f7070696e67436172742f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mrprompt/ShoppingCart/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/0c5e4987fd3a02b6c8b48341b7092313b45ed6694fc05175f0204db02a819d84/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d7270726f6d70742f53686f7070696e67436172742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mrprompt/ShoppingCart/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/fde62b9a610915df48e9abaa369af02f352c0550e40d400f823af1ba5dcd07de/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d7270726f6d70742f53686f7070696e67436172742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mrprompt/ShoppingCart/?branch=master)[![Code Intelligence Status](https://camo.githubusercontent.com/df69c334eebf27f3796f9335ff466d20d29e68e0caa785f7074b25bac6eee930/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d7270726f6d70742f53686f7070696e67436172742f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)

A reusable shopping cart implementation.

### Install

[](#install)

```
composer require mrprompt/shopping-cart
```

### Using

[](#using)

#### Initializing a cart with items

[](#initializing-a-cart-with-items)

You can initializes a cart passing an array of items. An item is a instance of `Item` class or an implementation of `ItemInterface`.

```
use \MrPrompt\ShoppingCart\Cart;
use \MrPrompt\ShoppingCart\Item;

$items = [
    new Item(uniqid()),
    new Item(uniqid(), 1.00),
    new Item(uniqid(), 5.00, 10),
];

$cartId = uniqid();

$cart = new Cart($cartId, $items);
```

#### Add items to cart

[](#add-items-to-cart)

You can add items to an existent cart passing an instance of `Item`.

```
use \MrPrompt\ShoppingCart\Item;

$itemId = uniqid();
$itemPrice = 1.99;
$itemQuantity = 30;

$item = new Item($itemId, $itemPrice, $itemQuantity);

$cartId = uniqid();

$cart = new Cart($cartId);
$cart->addItem($item);
```

#### Remove an item from cart

[](#remove-an-item-from-cart)

Pass an instance of `Item` - previously added to cart obvously - to remove it.

```
use \MrPrompt\ShoppingCart\Item;

$itemId = uniqid();
$itemPrice = 1.99;
$itemQuantity = 30;

$item = new Item($itemId, $itemPrice, $itemQuantity);

$cartId = uniqid();
$cart = new Cart([ $item ]);
$cart->removeItem($item);
```

#### Cleanup the cart

[](#cleanup-the-cart)

To remove all items from cart, you can use the `cleanUp` method:

```
use \MrPrompt\ShoppingCart\Cart;
use \MrPrompt\ShoppingCart\Item;

$itemId = uniqid();
$itemPrice = 1.99;
$itemQuantity = 30;
$item = new Item($itemId, $itemPrice, $itemQuantity);

$cartId = uniqid();
$cart = new Cart($cartId, [ $item ]);
echo $cart->count(); // === 1

$cart->cleanUp();

echo $cart->count(); // === 0
```

#### Check if cart is empty

[](#check-if-cart-is-empty)

To check if the cart is empty, you can use `isEmpty` method:

```
use \MrPrompt\ShoppingCart\Cart;
use \MrPrompt\ShoppingCart\Item;

$itemId = uniqid();
$itemPrice = 1.99;
$itemQuantity = 30;
$item = new Item($itemId, $itemPrice, $itemQuantity);

$cartId = uniqid();

$cart = new Cart($cartId, [ $item ]);
echo $cart->isEmpty(); // === false

$cart->cleanUp();

echo $cart->isEmpty(); // === true
```

### Testing

[](#testing)

```
phpunit --coverage-text --testdox
```

#### MIT License

[](#mit-license)

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/9d08744966e0d5783569605da853ef945dadac0be0f613f2eba6a052861d937a?d=identicon)[mrprompt](/maintainers/mrprompt)

---

Top Contributors

[![mrprompt](https://avatars.githubusercontent.com/u/166088?v=4)](https://github.com/mrprompt "mrprompt (17 commits)")

### Embed Badge

![Health badge](/badges/mrprompt-shopping-cart/health.svg)

```
[![Health](https://phpackages.com/badges/mrprompt-shopping-cart/health.svg)](https://phpackages.com/packages/mrprompt-shopping-cart)
```

###  Alternatives

[icecave/duct

An incremental streaming JSON parser.

773.5k1](/packages/icecave-duct)[aimes/module-checkout-designs

Swap checkout layouts via system configuration, or conditions

3312.2k1](/packages/aimes-module-checkout-designs)[jimbojsb/emogrify

CSS inliner for HTML Emails. Great with Swiftmailer and Zend Mail

1627.6k1](/packages/jimbojsb-emogrify)

PHPackages © 2026

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