Skip to content

laravelir/cart

Repository files navigation

  • Starts
  • Forks
  • Total Downloads

cart package (In Develop)

a multi driver shopping cart package

Installation

  1. Run the command below to add this package:
composer require laravelir/cart
  1. Open your config/app.php and add the following to the providers/aliases array:
Laravelir\Cart\Providers\CartServiceProvider::class,
'Cart' => Laravelir\Cart\Facade\Cart::class,
  1. Run the command below to install package:
php artisan cart:install

Drivers

Session - Cookie - Eloquent - Cache

you can set your favorite driver in config file

eloquent driver need to authenticated user for save user's id

How to use

add HasCart trait to User model

this trait has one method:

$this->cart(); // return cart 

Methods

$cart = resolve(Cart());

$cart->all();

$cart->add();

$cart->has();

$cart->update();

$cart->count();

$cart->delete();

$cart->truncate();

Helpers

cart();

cartItems();

Cart::user($user_id)->add(); Cart::driver('session')->add(); Cart::add(['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => ['size' => 'large']]);

Usage

$user = auth()->user();

$product = Product::first();

$cart = resolve(Cart());

$data = [ 'item' => $product, 'title' => $product->title, 'quantity' => 1, 'price' => $product->price, ];

$options = [ 'color' => 'red', 'size' => 'XL' ]; $user->cart->add($data, $options);

$data = [

]; Cart::update($itemId, $data); Cart::update($itemId)->increaseQuantity(); Cart::update($itemId)->decreaseQuantity(); Cart::all(); Cart::count(); Cart::get($itemId); Cart::delete($itemId); Cart::truncate();

Cartable Contract

For the convenience of faster adding items to cart and their automatic association, your model can implement Buyable interface. To do so, it must implement such functions:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Product exends Model implements Cartable {

    public function getCartableId() {
        return $this->id;
    }

    public function getCartableTitle() {
        return $this->title;
    }

    public function getCartablePrice() {
        return $this->price;
    }
}

Events

The cart has following events:

cart.added - when an item added cart.updated - when an item cart updated cart.deleted - when an item deleted

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages