basketin/cart

Cart for eCommerce store

v1.0.0-beta3 2024-05-06 17:00 UTC

This package is auto-updated.

Last update: 2024-05-06 17:02:09 UTC


README

cover.svg

Source Packagist Version

Basketin Cart

Cart module for eCommerce system based on Laravel.

Documentation

Installation

Install via composer.

composer require basketin/cart

You need to migrate the package tables.

php artisan migrate --path=/vendor/basketin/cart/database/migrations

If you need to auth migrate without set path you can set true to basketin.cart.setup.auto_migrate at config.

Publish config

php artisan vendor:publish --tag=basketin-cart-config

How to use

Create New Cart

<?php

use Basketin\Component\Cart\Facades\CartManagement;

$cart = CartManagement::initCart('01HF7V7N1MG9SDFPQYWXDNHR9Q', 'USD'); // <- ULID

You can open the cart if it exists or create a new cart if not exist.

Open Exist Cart

<?php

use Basketin\Component\Cart\Facades\CartManagement;

$cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID

Get Ulid

$cart->getUlid();

Get Currency

$cart->getCurrency();

Get Count Products

$cart->getCountProducts();

Get Count items

$cart->getCountItems();

Open the existing cart only

Add QuoteYou need to prepare a Product model to use like this.

// Product model
<?php

...
use Basketin\Component\Cart\Contracts\IQuote;
use Basketin\Component\Cart\Traits\HasQuote;
use Basketin\Component\Cart\Traits\HasTotal;

class Product extends Model implements IQuote
{
    use HasFactory;
    use HasQuote;
    use HasTotal;

    public function getOriginalPriceAttribute(): float
    {
        return (float) $this->price;
    }

    public function getSpecialPriceAttribute(): float|null
    {
        return null;
    }
}

Increase Quote

<?php

use App\Models\Product;
use Basketin\Component\Cart\Facades\CartManagement;

$product = Product::first();

$cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID
$cart->quote()->increaseQuote($product, 5);

Decrease Quote

<?php

use App\Models\Product;
use Basketin\Component\Cart\Facades\CartManagement;

$product = Product::first();

$cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID
$cart->quote()->decreaseQuote($product, 2);

Has Quote

<?php

use App\Models\Product;
use Basketin\Component\Cart\Facades\CartManagement;

$product = Product::first();

$cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID
$cart->quote()->hasQuote($product);

Remove Quote

<?php

use App\Models\Product;
use Basketin\Component\Cart\Facades\CartManagement;

$product = Product::first();

$cart = CartManagement::openCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID
$cart->quote()->removeQuote($product);

Get Cart

<?php

use Basketin\Component\Cart\Facades\CartManagement;

$cart = CartManagement::initCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID
$cart->getCart();

Get Quotes

<?php

use Basketin\Component\Cart\Facades\CartManagement;

$cart = CartManagement::initCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID
$cart->quote()->getQuotes();

Get Totals

<?php

use Basketin\Component\Cart\Facades\CartManagement;

$cart = CartManagement::initCart('01HF7V7N1MG9SDFPQYWXDNHR9Q'); // <- ULID
$totals = $cart->totals();
$totals->getSubTotal();
$totals->getDiscountTotal();
$totals->getGrandTotal();

If you need to add a global discount to the cart you can use it.

$totals->setGlobalDiscountTotal(500.00)
    ->getGrandTotal();

Coupon

Coupon model

You need to prepare a coupon model to inject into cart services

use Illuminate\Database\Eloquent\Model;
use Basketin\Component\Cart\Contracts\ICoupon;

class Coupon extends Model implements ICoupon
{
    protected $fillable = [
        'coupon_name',
        'coupon_code',
        'discount_type',
        'discount_value',
        'start_at',
        'ends_at',
    ];

    public function discountType(): String
    {
        return $this->discount_type;
    }

    public function discountValue(): Int
    {
        return $this->discount_value;
    }
}

The discount type: fixed = CouponCalculate::FIXED | percent = CouponCalculate::PERCENT

To apply coupon code on cart:-

$coupon = Coupon::first();
$cart->coupon($coupon);

Fields

You can create fields that contain a key and values for each shopping cart.

Set Field

return $cart->fields()->set('key', 'value');

Get Field

return $cart->fields()->get('key');

Remove

return $cart->fields()->remove('key');

Has Field

return $cart->fields()->has('key');

Contributing

Thank you for considering contributing to this package! Be one of the Store team.

License

This package is an open-sourced software licensed under the MIT license.