This package is a modular and extensible library designed to strengthen cart management within the Laravel ecosystem. It is classified under the following headings:
- Core Cart: Functions to add, remove, update quantity, and clear cart items.
- Persistence Layer: Durable storage via a migration for
cart_itemswith a JSON-enabledoptionscolumn and theCartItemmodel. - Interface & Contracts: Abstractions through
CartInterface,ProductResolverInterface, andTaxCalculatorInterface. - Events System:
ItemAdded,ItemUpdated,ItemRemoved, andCartClearedevents. - Calculators: Subtotal, tax, and total calculation via
DefaultCartCalculatorandDefaultTaxCalculator. - Configurable Resolver: Configure the model and tax rate (
tax_rate) inconfig/cart.php. - Actions: Extendable action framework with examples like
MailActionfor sending emails. - Facade & Helpers: Provides a
Cartfacade and helper functions.
-
Cart Management
- Add/remove/update/clear items (
addItem,removeItem,updateQuantity,clear) - List items (
items) and calculate total (total)
- Add/remove/update/clear items (
-
Database Support
cart_itemstable:cart_id(UUID),item_id,quantity,unit_price,options(JSON), timestamps
-
Event Dispatch
ItemAdded,ItemUpdated,ItemRemoved,CartCleared
-
Actions
- Example
MailAction—send email with sender, recipient, subject, and content parameters
- Example
-
Configuration
- Configure model and
tax_rateviaconfig/cart.php
- Configure model and
-
Calculators
DefaultCartCalculator: subtotal, tax, and grand totalDefaultTaxCalculator: percentage-based tax calculation
-
Facade
- Provides the
Cartfacade
- Provides the
-
Require the package via Composer:
composer require taskinbirtan/ecommerce.cart
-
Register the service provider and facade (if auto-discovery is disabled) in
config/app.php:Taskinbirtan\EcommerceCart\CartServiceProvider::class, 'Cart' => Taskinbirtan\EcommerceCart\Facades\Cart::class,
-
Publish the configuration file:
php artisan vendor:publish --provider="Taskinbirtan\EcommerceCart\CartServiceProvider" --tag=config -
Run the migrations:
php artisan migrate
Note: The default migration file is named
1919_05_19_commemorate_ataturk.phpin honor of Atatürk and to celebrate Youth and Sports Day, commemorating the beginning of the Turkish War of Independence and marking the date this project was initiated.
config/cart.php:
return [
'product_model' => App\\Models\\Product::class,
'tax_rate' => 18, // percentage
];use Cart;
Cart::addItem($productId, $quantity = 1, ['color' => 'red']);Cart::removeItem($productId);Cart::updateQuantity($productId, $newQuantity);Cart::clear();$items = Cart::items();
$total = Cart::total();You can listen to the following events to react to cart changes:
Taskinbirtan\EcommerceCart\Events\ItemAddedTaskinbirtan\EcommerceCart\Events\ItemUpdatedTaskinbirtan\EcommerceCart\Events\ItemRemovedTaskinbirtan\EcommerceCart\Events\CartCleared
Example listener:
public function handle(ItemAdded $event)
{
// $event->item
}This package can be tested with Pest & PHPUnit:
vendor/bin/pestIn future versions, we aim to cover e-commerce components beyond Cart with the following modules:
- Product Catalog: CRUD operations, detail pages, variation support (color, size, etc.)
- Category Management: Multi-level category/ subcategory hierarchy and category-based filtering
- Inventory & Stock Tracking: Warehouse-based stock management, critical stock alerts
- Order Management: Creating orders, status updates, and customer notifications
- Payment Integrations: Stripe, PayPal, local banks, and installment options
- Shipping & Logistics: Carrier API integration, shipment tracking
- Coupons & Discounts: Percentage- and amount-based promo codes, usage rules
- Customer Accounts: Registration/login, profile, address book, order history
- Invoicing & Returns: PDF invoice generation, return request management
- Multi-Language & Multi-Currency: Localization infrastructure, currency rate updates
- Reporting & Analytics: Sales, customer, and stock reports with graphical displays
- Search & Filtering Infrastructure: Elasticsearch/Algolia integration, advanced filters
- UI/UX Components: Responsive theme and demo components (Tailwind, jQuery)
- Security & Performance: Rate limiting, cache (Redis), scalable architecture recommendations
- Fork the repository
- Create a new branch (
git checkout -b feature/fooBar) - Commit your changes (
git commit -am 'Add some fooBar') - Push to the branch (
git push origin feature/fooBar) - Open a pull request
If you like this package and want to support its development, you can buy me a coffee! ☕
MIT © taskinbirtan