darkraul79/cartify
最新稳定版本:v0.1.1
Composer 安装命令:
composer require darkraul79/cartify
包简介
A flexible and powerful shopping cart package for Laravel applications
README 文档
README
A flexible and powerful shopping cart package for Laravel
⚠️ Alpha Version (0.1.x) - This package is in early development. APIs may change. Use with caution in production.
Features
- ✅ Simple and intuitive API
- ✅ Automatic price calculations (subtotal, tax, total)
- ✅ Multiple cart instances (cart, wishlist, etc.)
- ✅ Persistent cart for authenticated users
- ✅ Session-based storage
- ✅ Database migrations included
- ✅ Flexible and extensible
- ✅ Laravel 12+ support
Installation
composer require darkraul79/cartify
Publish the configuration and migrations:
php artisan vendor:publish --provider="Raulsdev\Cartify\CartifyServiceProvider"
Run migrations:
php artisan migrate
Configuration
Configure in .env:
CARTIFY_TAX_RATE=0.21 CARTIFY_CURRENCY=EUR CARTIFY_CURRENCY_SYMBOL=€
Or publish and edit config/cartify.php.
Quick Start
use Darkraul79\Cartify\Facades\Cart; // Add item to cart Cart::add( id: 1, name: 'Product Name', quantity: 2, price: 29.99, options: ['color' => 'red', 'size' => 'M'] ); // Get cart content $items = Cart::content(); // Calculate totals $subtotal = Cart::subtotal(); $tax = Cart::tax(0.21); // 21% tax $total = Cart::total(0.21); // Update quantity Cart::update(id: 1, quantity: 3); // Remove item Cart::remove(id: 1); // Clear cart Cart::clear(); // Count items $count = Cart::count();
Usage
Multiple Instances
Use different cart instances for cart, wishlist, etc:
// Shopping cart Cart::instance('cart')->add(1, 'Product A', 1, 29.99); // Wishlist Cart::instance('wishlist')->add(2, 'Product B', 1, 49.99); // Get wishlist content $wishlist = Cart::instance('wishlist')->content();
User Persistence
Store and restore cart for authenticated users:
// On login Cart::restore(); // Restore saved cart // On logout Cart::store(); // Save cart
Merge Carts
Combine session cart with stored cart:
Cart::merge(); // Merge session cart with stored cart
Search Cart
$redItems = Cart::search(function ($item) { return $item['options']['color'] === 'red'; });
Helper Functions
// Get cart instance $cart = cart(); $wishlist = cart('wishlist'); // Format price echo format_price(29.99); // "29,99 €" // Generate order number $orderNumber = generate_order_number(); // "ORD-202501-A3F9E2"
API Reference
Adding Items
Cart::add( id: int|string, name: string, quantity: int = 1, price: float = 0, options: array = [] ): void
Updating Items
Cart::update(id: int|string, quantity: int): void
Removing Items
Cart::remove(id: int|string): void
Getting Cart Data
Cart::content(): Collection Cart::get(id: int|string): ?array Cart::has(id: int|string): bool Cart::count(): int Cart::isEmpty(): bool
Calculations
Cart::subtotal(): float Cart::tax(?float $taxRate = null): float Cart::total(?float $taxRate = null): float
Cart Management
Cart::clear(): void Cart::instance(?string $name = null): CartManager Cart::store(?int $userId = null): void Cart::restore(?int $userId = null): void Cart::merge(?int $userId = null): void
Example Integration
Controller
use Raulsdev\Cartify\Facades\Cart; use App\Models\Product; class CartController extends Controller { public function add(Request $request) { $product = Product::findOrFail($request->product_id); Cart::add( id: $product->id, name: $product->name, quantity: $request->quantity ?? 1, price: $product->price ); return redirect()->route('cart.index'); } public function index() { return view('cart.index', [ 'items' => Cart::content(), 'total' => Cart::total(0.21), ]); } }
Blade View
@foreach(Cart::content() as $item) <div> <h3>{{ $item['name'] }}</h3> <p>Price: {{ format_price($item['price']) }}</p> <p>Quantity: {{ $item['quantity'] }}</p> </div> @endforeach <p>Total: {{ format_price(Cart::total(0.21)) }}</p>
Database Schema
The package includes a migration for persistent cart storage:
cart_itemstable with columns:session_id- For guest usersuser_id- For authenticated usersproduct_id- Product referencename,quantity,priceoptions- JSON field for additional datainstance- Cart instance name
Testing
composer test
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License. See LICENSE for details.
Credits
Support
If you find this package helpful, please ⭐ star the repository!
For issues or questions, open an issue on GitHub.
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-19