basketin/cart

Cart for eCommerce store

v1.0.0-beta10 2024-09-03 07:02 UTC

README

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;
    }
}

Add Quote

<?php

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

$product = Product::first();

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

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);

And you can use $this->couponInfo() to get coupon info

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.