Usage
v1.xLearn how to manage carts, quotes, totals, coupons, and more.
Quick Start
use Obelaw\Basketin\Cart\Facades\CartManagement;
// Create or open a cart
$cart = CartManagement::make($ulid, 'USD', 'ORDER');
// Open existing cart only (throws CartNotFoundException if not found)
$cart = CartManagement::make($ulid, 'USD', 'ORDER', false);
Cart Methods
$cart->getUlid(); // Get cart ULID
$cart->getCurrency(); // Get currency code
$cart->getType(); // Get cart type
$cart->getCountProducts(); // Number of distinct products
$cart->getCountItems(); // Total item quantity
Quotes (Line Items)
Preparing Your Model
Your purchasable model must implement IQuote and use the required traits:
use Illuminate\Database\Eloquent\Model;
use Obelaw\Basketin\Cart\Contracts\IQuote;
use Obelaw\Basketin\Cart\Traits\HasQuote;
use Obelaw\Basketin\Cart\Traits\HasTotal;
class Product extends Model implements IQuote
{
use HasQuote, HasTotal;
public function getOriginalPriceAttribute(): float
{
return (float) $this->price;
}
public function getSpecialPriceAttribute(): ?float
{
return null; // Optional discounted price
}
}
Managing Quotes
use Obelaw\Basketin\Cart\Facades\CartManagement;
$cart = CartManagement::make($ulid, 'USD', null, false);
// Add, modify, and remove quotes
$cart->quote()->addQuote($product, 1);
$cart->quote()->increaseQuote($product, 5);
$cart->quote()->decreaseQuote($product, 2);
$cart->quote()->removeQuote($product);
// Check and retrieve quotes
$exists = $cart->quote()->hasQuote($product);
$quotes = $cart->quote()->getQuotes();
Limits
- Default limit per quote: 5 items (configurable)
- Exceeding the limit throws
QuoteQuantityLimitException - Operating on non-existent quotes throws
QuoteNotFoundException
Totals
$totals = $cart->totals();
$subTotal = $totals->getSubTotal();
$discountTotal = $totals->getDiscountTotal();
$grandTotal = $totals->getGrandTotal();
// Apply global discount
$grandAfterGlobal = $totals->setGlobalDiscountTotal(500.00)->getGrandTotal();
Custom Fields
Attach key-value data to carts:
$cart->fields()->set('shipping_method', 'express');
$cart->fields()->get('shipping_method');
$cart->fields()->has('shipping_method');
$cart->fields()->remove('shipping_method');
Order Checkout
// Prepare order and get cart order model
$order = $cart->preparingOrder();
// Associate with your order
$yourOrder->cartOrder()->save($order);
$cart->syncOrder($yourOrder);
// Complete checkout
$cart->checkoutIt('ORDER');
Events
| Event | Description |
|---|---|
BasketinCreateCartEvent | Cart initialized |
BasketinAddedQuoteEvent | Quote added |
BasketinIncreaseQuoteEvent | Quote quantity increased |
BasketinDecreaseQuoteEvent | Quote quantity decreased |
BasketinRemoveQuoteEvent | Quote removed |
Configuration
// config/basketin/cart.php
'setup' => [
'auto_migrate' => false,
],
'limit_quote' => 5,
Override at runtime:
use Obelaw\Basketin\Cart\Settings\Config;
$cart->config(new Config([
'limit_quote' => 15,
]));
Testing
composer test