All Projects → treeStoneIT → Shopping Cart

treeStoneIT / Shopping Cart

Licence: mit
An easy-to-use shopping cart for Laravel

Projects that are alternatives of or similar to Shopping Cart

Aimeos Laravel
Laravel ecommerce package for professional, ultra fast online shops, complex B2B applications and #gigacommerce
Stars: ✭ 5,204 (+9029.82%)
Mutual labels:  e-commerce, ecommerce-framework, laravel, laravel-package, shopping-cart
Aimeos
Integrated online shop based on Laravel 8 and the Aimeos e-commerce framework
Stars: ✭ 2,354 (+4029.82%)
Mutual labels:  e-commerce, laravel, shopping-cart
Ecommerce Laravel Bootstrap
Responsive, Multi-Vendor, MultiLanguage Online Store Platform (shopping cart solution)
Stars: ✭ 99 (+73.68%)
Mutual labels:  e-commerce, shopping-cart, laravel
Laravel Shopr
A developer-friendly e-commerce foundation for your Laravel app
Stars: ✭ 196 (+243.86%)
Mutual labels:  e-commerce, shopping-cart, laravel
S Cart
This project has been replaced by https://github.com/s-cart/s-cart
Stars: ✭ 258 (+352.63%)
Mutual labels:  e-commerce, shopping-cart, laravel
Laracom
Laravel FREE E-Commerce Software
Stars: ✭ 1,570 (+2654.39%)
Mutual labels:  e-commerce, shopping-cart, laravel
Bagisto
An easy to use, free and open source laravel eCommerce platform to build your online shop in no time.
Stars: ✭ 4,140 (+7163.16%)
Mutual labels:  ecommerce-framework, laravel, laravel-package
Framework
The truly Laravel E-commerce Framework
Stars: ✭ 456 (+700%)
Mutual labels:  e-commerce, laravel, laravel-package
Framework
An eCommerce administration built with Laravel 7 for create and manage online shop with multi-vendor.
Stars: ✭ 56 (-1.75%)
Mutual labels:  laravel, laravel-package
Laravel Database Logger
Log database query sql in Laravel Application, support Guard,Auth to multiple file record
Stars: ✭ 31 (-45.61%)
Mutual labels:  laravel, laravel-package
Laravel Weather
🌤️ A wrapper around Open Weather Map API (Current weather)
Stars: ✭ 36 (-36.84%)
Mutual labels:  laravel, laravel-package
Doorman
Limit access to your Laravel applications by using invite codes
Stars: ✭ 913 (+1501.75%)
Mutual labels:  laravel, laravel-package
Chatify
A Laravel package that allows you to add a complete user messaging system into your new/existing Laravel application.
Stars: ✭ 885 (+1452.63%)
Mutual labels:  laravel, laravel-package
Cms
Statamic 3: The Core Composer Package
Stars: ✭ 965 (+1592.98%)
Mutual labels:  laravel, laravel-package
Laravel Mailguneu
Allow customising the Mailgun server URL to use EU servers.
Stars: ✭ 13 (-77.19%)
Mutual labels:  laravel, laravel-package
Laravel Compass
A REST client inside your Laravel app
Stars: ✭ 1,002 (+1657.89%)
Mutual labels:  laravel, laravel-package
Larapi
💛 Modern API development in Laravel.
Stars: ✭ 54 (-5.26%)
Mutual labels:  laravel, laravel-package
Laravelshoppingcart
Shopping Cart Implementation for Laravel Framework
Stars: ✭ 853 (+1396.49%)
Mutual labels:  shopping-cart, laravel
Laravel Qrcode Ecommerce
This is a complete laravel project that handles qrcodes, payments, api/microservices, and ecommerce
Stars: ✭ 36 (-36.84%)
Mutual labels:  laravel, laravel-package
Laravel Settings
Simple Settings package for a laravel application
Stars: ✭ 45 (-21.05%)
Mutual labels:  laravel, laravel-package

A Database Driven Shopping Cart for Laravel

Software License Latest Version on Packagist Total Downloads Build Status StyleCI Scrutinizer Code Quality

This is a simple shopping cart implementation for Laravel 6/7/8. It automatically serializes your cart to the database and loads the related product models.

Usage

To get started, add the Buyable interface to your model.

use Illuminate\Database\Eloquent\Model;
use Treestoneit\ShoppingCart\Buyable;
use Treestoneit\ShoppingCart\BuyableTrait;

class Product extends Model implements Buyable
{
    use BuyableTrait;
}

Make sure you implement the getBuyableDescription and getBuyablePrice methods with the respective product description and product price.

Now you can add products to the cart.

use Treestoneit\ShoppingCart\Facades\Cart;

$product = Product::create(['name' => 'Pizza Slice', 'price' => 1.99]);
$quantity = 2;

Cart::add($product, $quantity);

To retrieve the cart contents:

Cart::content();
// or
Cart::items();

To retrieve the total:

Cart::subtotal();

You can update the quantity of an item in the cart. The first argument is the primary id of the related CartItem.

$item = Cart:content()->first();

Cart::update($item->id, $item->quantity + 5);

Or remove the item completely.

Cart::remove($item->id);

Options

To add item-specific options (such as size or color) to an item in the cart, first register available options in your Buyable instance.

class Product extends Model implements Buyable
{
    // ...
    
    public function getOptions(): array
    {
        return [
            'size' => ['18 inch', '36 inch'],
            'color' => ['white', 'blue', 'black'],
        ];
    }
}

Then you just pass an associative array as the third parameter of Cart::add.

Cart::add($product, 3, ['color' => 'white']);

Any invalid options will be silently removed from the array.

You can also add or change options of an item currently in the cart by calling Cart::updateOption.

$item = Cart:content()->first();

// Update a single option
Cart::updateOption($item->id, 'color', 'black');

// Update multiple options at once
Cart::updateOptions($item->id, [
    'color' => 'black',
    'size' => '36 inch',
]);

The options array will be available on the CartItem instance as $item->options.

Attaching to Users

You can attach a cart instance to a user, so that their cart from a previous session can be retrieved. Attaching a cart to a user is acheived by calling the attachTo method, passing in an instance of Illuminate\Contracts\Auth\Authenticatable.

class RegisterController
{
    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        Cart::attachTo($user);
    }
}

Then when the user logs in, you can call the loadUserCart method, again passing the user instance.

class LoginController
{
    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        Cart::loadUserCart($user);
    }
}

Dependency Injection

If you're not a facade person, you can use the container to inject the shopping cart instance by type-hinting the Treestoneit\ShoppingCart\CartManager class, or the Treestoneit\ShoppingCart\CartContract interface.

Tax

The shopping cart can calculate the total tax of the items in the cart. Just call

$rate = 13; // The tax rate as a percentage

Cart::tax($rate);

You can also set a default tax rate in the included config file.

// config/shopping-cart.php

    'tax' => [
        'rate' => 6,
    ],

Then just call Cart::tax without a parameter.

Cart::tax();

If some of your items have different tax rates applicable to them, or are tax-free, no problem. First modify the config file:

// config/shopping-cart.php

    'tax' => [
        'mode' => 'per-item',
    ],

Then, set the tax rate per item by implementing the Taxable interface and defining a getTaxRate method.

use Treestoneit\ShoppingCart\Taxable;

class Product extends Model implements Buyable, Taxable
{
    /**
     * Calculate the tax here based on a database column, or whatever you will.
     *
     * @return int|float
     */
    public function getTaxRate()
    {
        if ($this->tax_rate) {
            return $this->tax_rate;
        }

        if (! $this->taxable) {
            return 0;
        }

        return 8;
    }

Now your items will have their custom tax rate applied to them when calling Cart::tax.

Installation

You can install the package via composer:

composer require treestoneit/shopping-cart

To publish the config file and migrations, run

php artisan vendor:publish --provider="Treestoneit\ShoppingCart\CartServiceProvider"

And run the included database migrations.

php artisan migrate

Testing

composer test

Starter (demo) Repository

If you would like to see a starter/demo implementation using this shopping cart please check out our laravel-commerce repository

Roadmap

Some things I didn't get around to yet:

  • Clear cart instance which has not been attached to a user when session is destroyed.
  • Add an Artisan command that will clear any unattached carts (these two might be mutually exclusive)
  • Add ability to configure cart merging strategy when loadUserCart is called

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].