PHPackages                             hnooz/laravel-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. [Framework](/categories/framework)
4. /
5. hnooz/laravel-shopping-cart

ActiveLibrary[Framework](/categories/framework)

hnooz/laravel-shopping-cart
===========================

A lightweight and flexible Laravel package for managing shopping carts with full support for cart items, session storage, and database persistence.

1.0.0(1y ago)0538[4 PRs](https://github.com/hnooz/laravel-shopping-cart/pulls)MITPHPPHP ^8.3CI passing

Since May 24Pushed 4d ago1 watchersCompare

[ Source](https://github.com/hnooz/laravel-shopping-cart)[ Packagist](https://packagist.org/packages/hnooz/laravel-shopping-cart)[ Docs](https://github.com/hnooz/laravel-shopping-cart)[ GitHub Sponsors](https://github.com/Hnooz)[ RSS](/packages/hnooz-laravel-shopping-cart/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (9)Versions (6)Used By (0)

Laravel Cart
============

[](#laravel-cart)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e32f8d64ab7265dc2082f86f1f2297a3bbe11044372fd81b0ffcb9f4e3d5a8c2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686e6f6f7a2f6c61726176656c2d73686f7070696e672d636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hnooz/laravel-shopping-cart)[![GitHub Tests Action Status](https://camo.githubusercontent.com/739a302613930202e9928a50a1ba78793569ba7125b67d5d77af28b0f448ef39/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f686e6f6f7a2f6c61726176656c2d73686f6f70696e672d636172742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/hnooz/laravel-shopping-cart/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/6442084cf69fa61dafd0227d63034f8686d71b9a0042201810c206f2248c1379/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f686e6f6f7a2f6c61726176656c2d73686f7070696e672d636172742f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/hnooz/laravel-shopping-cart/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/75d9419b1ae95f4974dbbb979be3788021b536d788ad17607ef7d5f8b205dde8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686e6f6f7a2f6c61726176656c2d73686f6f70696e672d636172742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hnooz/laravel-shopping-cart)

A flexible and easy-to-use Laravel shopping cart package that supports both database and session storage. Perfect for e-commerce applications that need to handle both guest and authenticated user shopping experiences.

Features
--------

[](#features)

- **Flexible Storage**: Database, session, or hybrid storage options
- **Multi-User Support**: Seamlessly handles guest and authenticated users
- **Easy-to-Use Facade**: Simple, intuitive API
- **Item Management**: Add, remove, increase/decrease quantities
- **Calculations**: Automatic totals and item counts
- **Well Tested**: Comprehensive test coverage with Pest
- **Laravel 12 Ready**: Built for the latest Laravel version
- **Clean Code**: Uses Rector and Pint for code quality

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

[](#installation)

You can install the package via composer:

```
composer require hnooz/laravel-shopping-cart
```

### Publish and Run Migrations

[](#publish-and-run-migrations)

Publish the migration file and run migrations:

```
php artisan vendor:publish --tag="laravel-cart-migrations"
php artisan migrate
```

### Publish Configuration (Optional)

[](#publish-configuration-optional)

If you want to customize the configuration:

```
php artisan vendor:publish --tag="laravel-cart-config"
```

Configuration
-------------

[](#configuration)

The package comes with sensible defaults, but you can customize the behavior by publishing the config file:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Cart Storage Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the default cart storage driver that will be used
    | to store cart items. You may set this to any of the storage options
    | listed below.
    |
    | Supported: "database", "session", "both"
    |
    */
    'driver' => env('CART_DRIVER', 'both'),

    /*
    |--------------------------------------------------------------------------
    | Cart Database Connection
    |--------------------------------------------------------------------------
    |
    | This is the database connection that will be used to store cart items
    | when using the "database" or "both" storage driver.
    |
    */
    'connection' => env('CART_DB_CONNECTION', null),

    /*
    |--------------------------------------------------------------------------
    | Cart Items Table
    |--------------------------------------------------------------------------
    |
    | This is the table that will be used to store cart items when using
    | the "database" or "both" storage driver.
    |
    */
    'table' => 'cart_items',

    /*
    |--------------------------------------------------------------------------
    | Session Key
    |--------------------------------------------------------------------------
    |
    | This is the session key that will be used to store cart items when
    | using the "session" or "both" storage driver.
    |
    */
    'session_key' => 'shopping_cart',
];
```

Usage
-----

[](#usage)

### Basic Operations

[](#basic-operations)

```
use Hnooz\LaravelCart\Facades\Cart;

// Add items to cart
Cart::add('product-1', 'iPhone 14', 999.99, 1);
Cart::add('product-2', 'MacBook Pro', 1999.99, 2, ['color' => 'Space Gray']);

// Remove an item
Cart::remove('product-1');

// Increase item quantity
Cart::increase('product-2', 1); // Adds 1 more MacBook Pro

// Decrease item quantity
Cart::decrease('product-2', 1); // Removes 1 MacBook Pro

// Get all cart items
$items = Cart::all();

// Get item count
$count = Cart::count(); // Total quantity of all items

// Get cart total
$total = Cart::total(); // Total price of all items

// Clear entire cart
Cart::clear();
```

### Working with Item Options

[](#working-with-item-options)

You can store additional data with each cart item:

```
Cart::add('shirt-001', 'Cotton T-Shirt', 29.99, 2, [
    'size' => 'L',
    'color' => 'Blue',
    'customization' => 'Custom text on back'
]);

$items = Cart::all();
foreach ($items as $item) {
    echo $item['name'] . ' - Size: ' . $item['options']['size'];
}
```

### Storage Drivers

[](#storage-drivers)

#### Session Storage

[](#session-storage)

Best for simple applications or when you don't need persistent carts:

```
// In config/cart.php or .env
'driver' => 'session'
// or
CART_DRIVER=session
```

#### Database Storage

[](#database-storage)

Best for when you need persistent carts and user account integration:

```
// In config/cart.php or .env
'driver' => 'database'
// or
CART_DRIVER=database
```

#### Hybrid Storage (Recommended)

[](#hybrid-storage-recommended)

Uses session for guests and database for authenticated users:

```
// In config/cart.php or .env
'driver' => 'both'
// or
CART_DRIVER=both
```

### Guest to User Cart Migration

[](#guest-to-user-cart-migration)

When a guest user logs in, their session cart can be easily migrated:

```
// In your authentication logic
use Hnooz\LaravelCart\Facades\Cart;

// After user login
if (session()->has('shopping_cart')) {
    // Cart items are automatically available
    // The package handles the transition seamlessly
}
```

API Reference
-------------

[](#api-reference)

### `Cart::add(string $id, string $name, float $price, int $quantity = 1, array $options = [])`

[](#cartaddstring-id-string-name-float-price-int-quantity--1-array-options--)

Adds an item to the cart. If the item already exists, the quantity will be increased.

**Parameters:**

- `$id` - Unique identifier for the item
- `$name` - Display name of the item
- `$price` - Price per unit
- `$quantity` - Quantity to add (default: 1)
- `$options` - Additional data array (default: \[\])

### `Cart::remove(string $id)`

[](#cartremovestring-id)

Removes an item completely from the cart.

### `Cart::increase(string $id, int $quantity = 1)`

[](#cartincreasestring-id-int-quantity--1)

Increases the quantity of an existing item.

### `Cart::decrease(string $id, int $quantity = 1)`

[](#cartdecreasestring-id-int-quantity--1)

Decreases the quantity of an existing item. Minimum quantity is 1.

### `Cart::clear()`

[](#cartclear)

Removes all items from the cart.

### `Cart::all()`

[](#cartall)

Returns all cart items as an array.

### `Cart::count()`

[](#cartcount)

Returns the total quantity of all items in the cart.

### `Cart::total()`

[](#carttotal)

Returns the total price of all items in the cart.

Advanced Usage
--------------

[](#advanced-usage)

### Using the Contract

[](#using-the-contract)

You can type-hint the contract in your classes:

```
use Hnooz\LaravelCart\Contracts\CartInterface;

class CheckoutService
{
    public function __construct(
        protected CartInterface $cart
    ) {}

    public function processOrder()
    {
        $items = $this->cart->all();
        $total = $this->cart->total();

        // Process order...

        $this->cart->clear();
    }
}
```

### Custom Cart Implementation

[](#custom-cart-implementation)

You can create your own cart implementation by implementing the `CartInterface`:

```
use Hnooz\LaravelCart\Contracts\CartInterface;

class CustomCartManager implements CartInterface
{
    // Implement all required methods...
}

// In a service provider
$this->app->bind(CartInterface::class, CustomCartManager::class);
```

Testing
-------

[](#testing)

The package comes with comprehensive tests. To run the tests:

```
composer test
```

Code Quality
------------

[](#code-quality)

The package uses several tools to maintain code quality:

```
# Run Pint for style fix
composer style-fix

# Run rector for code improvements
composer rector-dry
composer rector-fix
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

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

Credits
-------

[](#credits)

- [Mohamed Idris](https://github.com/hnooz)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

42

—

FairBetter than 89% of packages

Maintenance76

Regular maintenance activity

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

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

Unknown

Total

1

Last Release

398d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5015db7c565fa0cd3839061cf59e75dbf9331cc9a4ac92ee0f52fe7955917400?d=identicon)[Hnooz](/maintainers/Hnooz)

---

Top Contributors

[![hnooz](https://avatars.githubusercontent.com/u/44949769?v=4)](https://github.com/hnooz "hnooz (19 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

laravelcartLaravel carthnooz

###  Code Quality

TestsPest

Static AnalysisRector

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.3M42](/packages/spatie-laravel-pdf)[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.1k9.9M90](/packages/dedoc-scramble)[spatie/laravel-health

Monitor the health of a Laravel application

87411.3M153](/packages/spatie-laravel-health)[lunarstorm/laravel-ddd

A Laravel toolkit for Domain Driven Design patterns

18476.4k](/packages/lunarstorm-laravel-ddd)[stephenjude/filament-jetstream

A Laravel starter kit built with Filament inspired by Jetstream.

17758.9k2](/packages/stephenjude-filament-jetstream)[spatie/laravel-passkeys

Use passkeys in your Laravel app

470755.5k32](/packages/spatie-laravel-passkeys)

PHPackages © 2026

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