4.7 KiB
Shop and Cart Facades Implementation Summary
Overview
Successfully implemented two core Facades for the Laravel Shop package to simplify the API for Laravel developers.
Files Created
1. Facades
src/Facades/Shop.php
- Static accessor for shop-related functionality
- Provides convenient methods for product browsing and inventory management
- Type-hinted methods for IDE autocomplete support
src/Facades/Cart.php
- Static accessor for shopping cart operations
- Simplifies cart management without needing authentication context
- Type-hinted methods for IDE autocomplete support
2. Services
src/Services/ShopService.php
Core implementation for shop operations:
products()- Get all products query builderproduct($id)- Get single productcategories()- Get all categoriesinStock()- Get in-stock productsfeatured()- Get featured productspublished()- Get published and visible productssearch($query)- Search productscheckStock($product, $quantity)- Verify stock availabilitygetAvailableStock($product)- Get available quantityisOnSale($product)- Check if product is on saleconfig($key, $default)- Get shop configurationcurrency()- Get default currency
src/Services/CartService.php
Core implementation for cart operations:
current()- Get current authenticated user's cartforUser($user)- Get cart for specific userfind($cartId)- Find cart by IDadd($product, $quantity, $parameters)- Add item to cartremove($product, $quantity, $parameters)- Remove item from cartupdate($cartItem, $quantity)- Update item quantityclear()- Clear cartcheckout()- Checkout carttotal()- Get cart totalitemCount()- Get item countitems()- Get cart itemsisEmpty()- Check if emptyisExpired()- Check if expiredisConverted()- Check if convertedunpaidAmount()- Get unpaid amountpaidAmount()- Get paid amount
3. Service Provider Updates
Updated src/ShopServiceProvider.php to:
- Bind
shop.servicetoShopServicein the container - Bind
shop.carttoCartServicein the container - Register both facades for easy access throughout the application
Test Coverage
tests/Feature/ShopFacadeTest.php (23 tests)
Tests for Shop facade functionality:
- Product retrieval and filtering
- Category access
- Stock checking
- Search functionality
- Configuration access
- Query builder chaining
- Pagination support
tests/Feature/CartFacadeTest.php (26 tests)
Tests for Cart facade functionality:
- Cart retrieval and creation
- Adding items with parameters
- Removing items
- Updating quantities
- Cart clearing and checkout
- Total and count calculations
- Cart status checks
- Paid/unpaid amount tracking
- Multi-product operations
Test Results
✅ All 49 new tests pass ✅ All 391 total tests pass (including existing tests) ✅ 7 tests skipped (intentional) ✅ No regressions to existing functionality
Usage Examples
Shop Facade
use Blax\Shop\Facades\Shop;
// Get featured products
$featured = Shop::featured()->with('prices')->get();
// Check stock availability
if (Shop::checkStock($product, 2)) {
// Add to cart
}
// Search products
$results = Shop::search('laptop')->paginate(10);
// Get available stock
$available = Shop::getAvailableStock($product);
Cart Facade
use Blax\Shop\Facades\Cart;
// Add to cart
Cart::add($product, quantity: 2, parameters: ['size' => 'L']);
// Get cart info
$total = Cart::total();
$count = Cart::itemCount();
$items = Cart::items();
// Update and manage
Cart::update($cartItem, quantity: 5);
Cart::remove($product, quantity: 1);
// Checkout
$purchases = Cart::checkout();
Benefits
- Cleaner Code: No need for
auth()->user()->currentCart()->getTotal() - Better Testing: Easy to mock with
Cart::shouldReceive() - IDE Support: Static methods provide excellent autocomplete
- Consistent Interface: Unified API across the package
- Type Safety: All methods are properly type-hinted
- Documentation: Methods are self-documenting through type hints
Future Improvements
Consider implementing additional facades:
Inventory- For stock managementPurchase- For purchase operationsStripe- For payment processing
These were outlined in the FACADE_SUGGESTIONS.md document and can be implemented using the same pattern.
Integration
The facades are automatically registered in the service container through the ShopServiceProvider. They're ready to use immediately after the package is installed:
// No additional configuration needed!
use Blax\Shop\Facades\Shop;
use Blax\Shop\Facades\Cart;
Shop::featured();
Cart::add($product);