Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/binafy/laravel-cart
Laravel Cart is a customizable package for adding shopping cart functionality to Laravel applications
https://github.com/binafy/laravel-cart
basket binafy cart e-commerce laravel laravel-basket laravel-cart laravel-cart-binafy laravel-cart-package laravel-framework milwad php shop-cart
Last synced: 27 days ago
JSON representation
Laravel Cart is a customizable package for adding shopping cart functionality to Laravel applications
- Host: GitHub
- URL: https://github.com/binafy/laravel-cart
- Owner: binafy
- License: mit
- Created: 2024-05-31T10:16:04.000Z (5 months ago)
- Default Branch: 1.x
- Last Pushed: 2024-08-15T16:20:30.000Z (3 months ago)
- Last Synced: 2024-09-28T19:22:41.526Z (about 1 month ago)
- Topics: basket, binafy, cart, e-commerce, laravel, laravel-basket, laravel-cart, laravel-cart-binafy, laravel-cart-package, laravel-framework, milwad, php, shop-cart
- Language: PHP
- Homepage: https://packagist.org/packages/binafy/laravel-cart
- Size: 95.7 KB
- Stars: 145
- Watchers: 1
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Cart
[![PHP Version Require](https://img.shields.io/packagist/dependency-v/binafy/laravel-cart/php.svg)](https://packagist.org/packages/binafy/laravel-cart)
[![Latest Stable Version](https://img.shields.io/packagist/v/binafy/laravel-cart.svg)](https://packagist.org/packages/binafy/laravel-cart)
[![Total Downloads](https://img.shields.io/packagist/dt/binafy/laravel-cart.svg)](https://packagist.org/packages/binafy/laravel-cart)
[![License](https://img.shields.io/packagist/l/binafy/laravel-cart.svg)](https://packagist.org/packages/binafy/laravel-cart)
[![Passed Tests](https://github.com/binafy/laravel-cart/actions/workflows/tests.yml/badge.svg)](https://github.com/binafy/laravel-cart/actions/workflows/tests.yml)- [Introduction](#introduction)
- [Features](#features)
- [Installation](#installation)
- [Publish](#publish)
- [Usage](#usage)
- [Configuration](#configuration)
- [Laravel Cart Facade](#laravel-cart-facade)
- [Driver](#driver)
- [Support Drivers](#support-drivers)
- [Laravel Cart Model](#laravel-cart-model)
- [Store Cart](#store-cart)
- [Access Itemable](#access-itemable)
- [Create Cart With Storing Items](#create-cart-with-storing-item)
- [Store multiple items](#store-multiple-items)
- [Store Item For a Cart](#store-item-for-a-cart)
- [Delete Item From Cart](#delete-item-from-cart)
- [Delete All Items From Cart](#delete-all-items-from-cart)
- [Increase Quantity](#increase-quantity)
- [Decrease Quantity](#decrease-quantity)
- [Available Events](#available-events)
- [Contributors](#contributors)
- [Security](#security)
- [Changelog](#changelog)
- [License](#license)The `Laravel Cart` is a highly customizable and flexible package that integrates basket functionality into your Laravel application. It simplifies storing and managing cart items, supporting multiple item types and quantities. It is ideal for e-commerce platforms to create carts, attach items, and manage them efficiently. Installation is straightforward via Composer, and it offers robust features like secure item storage, easy cart manipulation, and seamless integration with your existing Laravel app.
The `Laravel Cart` package is an ideal choice for developers looking to implement a reliable and scalable cart system in their Laravel-based e-commerce applications:
- Item Management: Easily add, update, and remove items from the cart with an intuitive API.
- Attributes and Options: Define custom attributes and options for cart items to handle variations like size and color.
- Tax Calculation: Built-in support for tax calculations, enabling automatic tax application based on predefined rules.
- Discounts and Coupons: Integrate discount codes and coupon functionalities to offer promotions and special offers to customers.
- Session and Database Storage: Flexible storage options allowing carts to be stored in sessions or the database.
- Events and Listeners: Hook into various cart events with listeners to perform actions like logging or triggering additional business logic.
- Customizable: Extend and customize the core functionalities to meet specific business requirements.
- Easy Integration: Designed to integrate seamlessly with existing Laravel projects, providing a smooth development experience.You can install the package with Composer:
```bash
composer require binafy/laravel-cart
```If you want to publish a config file, you can use this command:
```shell
php artisan vendor:publish --tag="laravel-cart-config"
```If you want to publish the migrations, you can use this command:
```shell
php artisan vendor:publish --tag="laravel-cart-migrations"
```For convenience, you can use this command to publish config, migration, and ... files:
```shell
php artisan vendor:publish --provider="Binafy\LaravelCart\Providers\LaravelCartServiceProvider"
```After publishing, run the `php artisan migrate` command.
You can config the `Laravel Cart` with `laravel-cart.php` config that exists in `config` folder.
For convenience, you can use Laravel Cart facade to store, delete, and ...:
```php
storeItem($item, $userId|null);
LaravelCart::storeItem($item $userId|null);
```If you may to using Laravel Cart facade, you can change the driver for store, delete, and ...:
```php
storeItem($item, $userId|null);
LaravelCart::driver('session')->removeItem($item);
```> The default driver is `database` and if you would to change the driver, you need to use the Laravel Cart config file that exists on `config\laravel-cart.php`.
| Drivers | Name |
|----------|----------|
| Session | session |
| Database | database |Also, you are able to use Laravel Cart models for fetch or ... with Laravel Eloquent.
For storing a new cart, you can use `Cart` model:
```php
use \Binafy\LaravelCart\Models\Cart;$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
```If you want to access to itemable in `CartItem`, you can use `itemable` relation:
```php
$cartItem = new CartItem([
'itemable_id' => $itemable->id,
'itemable_type' => $itemable::class,
'quantity' => 1,
]);$cartItem->itemable()->first(); // Return Model Instance
```
### Create Cart With Storing Item> For storing an item for cart, you need to implement `Cartable` interface to your model.
```php
use Binafy\LaravelCart\Cartable;class Product extends Model implements Cartable
{
public function getPrice(): int
{
//
}
}
```After, you can store items in multiple ways:
```php
Cart::query()->firstOrCreateWithStoreItems(
item: $product,
quantity: 1,
userId: $user->id
);
```If you may to store multiple items for a cart, you can use `storeItems` method:
```php
$items = [
[
'itemable' => $product1,
'quantity' => 2,
],
[
'itemable' => $product2,
'quantity' => 1,
],
[
'itemable' => $product3,
'quantity' => 5,
],
];$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cart->storeItems($items);
```If you want to store items for cart, first you need to create a cart and attach items to cart:
```php
$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cartItem = new CartItem([
'itemable_id' => $itemable->id,
'itemable_type' => $itemable::class,
'quantity' => 1,
]);$cart->items()->save($cartItem);
```If you may to access the items of one cart, you can use `items` relation that exists in Cart model.
For conveniences, you can use `storeItem` method. This method take a model or array:
```php
$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);// Model
$cart->storeItem($itemable);// Array
$item['itemable'] = $itemable;
$item['quantity'] = 1;
$cart->storeItem($item);
```If you may to delete an item for a cart, you can use `removeItem` method:
```php
$items = [
[
'itemable' => $product1,
'quantity' => 2,
],
[
'itemable' => $product2,
'quantity' => 1,
],
[
'itemable' => $product3,
'quantity' => 5,
],
];$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cart->removeItem($product1);
```
### Delete All Items From CartIf you may to delete all items from a cart, you can use `emptyCart` method:
```php
$items = [
[
'itemable' => $product1,
'quantity' => 2,
],
[
'itemable' => $product2,
'quantity' => 1,
],
[
'itemable' => $product3,
'quantity' => 5,
],
];$cart = Cart::query()->firstOrCreate(['user_id' => $user->id]);
$cart->emptyCart();
```If you may to increase the quantity of item in cart, you can use `increaseQuantity` method:
```php
$cart->increaseQuantity(item: $item, quantity: 2); // By default quantity is 1
```If you may to decrease the quantity of item in cart, you can use `decreaseQuantity` method:
```php
$cart->decreaseQuantity(item: $item, quantity: 2); // By default quantity is 1
```The `Laravel Cart` have some events that you can listen to these event and doing something:
| Events | Description |
|------------------------------------|------------------------------------------------------------------|
| `LaravelCartStoreItemEvent` | When you store an item in cart, this event fired |
| `LaravelCartRemoveItemEvent` | When you remove an item from cart, this event fired |
| `LaravelCartEmptyEvent` | When you remove all items from cart, this event fired |
| `LaravelCartIncreaseQuantityEvent` | When you increase the quantity of item in cart, this event fired |
| `LaravelCartDecreaseQuantityEvent` | When you decrease the quantity of item in cart, this event fired |Thanks to all the people who contributed. [Contributors](https://github.com/binafy/laravel-cart/graphs/contributors).
If you discover any security-related issues, please email `[email protected]` instead of using the issue tracker.
The changelog can be found in the `CHANGELOG.md` file of the GitHub repository. It lists the changes, bug fixes, and improvements made to each version of the Laravel User Monitoring package.
The MIT License (MIT). Please see [License File](https://github.com/binafy/laravel-cart/blob/1.x/LICENSE) for more information.