Installation
composer require jmrashed/ecommerce
php artisan vendor:publish --provider="Jmrashed\Ecommerce\EcommerceServiceProvider" --tag="migrations"
php artisan migrate
php artisan ecommerce:install
php artisan ecommerce:install to generate config, seeders, and default assets.First Use Case: Adding a Product
use Jmrashed\Ecommerce\Models\Product;
// Create a simple product
$product = Product::create([
'name' => 'Premium T-Shirt',
'slug' => 'premium-tshirt',
'price' => 29.99,
'description' => 'High-quality cotton t-shirt',
'sku' => 'TSHIRT-001',
'stock' => 50,
]);
// Add a variant (e.g., size/color)
$product->variants()->create([
'name' => 'Black - Medium',
'price' => 29.99,
'sku' => 'TSHIRT-001-BK-M',
'stock' => 30,
]);
/admin/ecommerce (configured in config/ecommerce.php).config/ecommerce.php (payment gateways, currencies, tax settings).php artisan ecommerce:seed (populate demo data).php artisan ecommerce:clear-cache (clear compiled views/cache).resources/views/vendor/ecommerce/ (customize templates).routes/api.php (predefined endpoints for products, cart, orders).// Create a product with attributes (e.g., color, size)
$product = Product::create([
'name' => 'Wireless Earbuds',
'price' => 99.99,
]);
$product->attributes()->createMany([
['name' => 'Color', 'value' => 'Black'],
['name' => 'Color', 'value' => 'White'],
]);
// Assign variants dynamically
$product->variants()->create([
'name' => 'Black',
'price' => 99.99,
'sku' => 'EBUD-001-BK',
'stock' => 100,
'attribute_values' => ['Black'],
]);
php artisan ecommerce:import with a CSV file (configured in config/ecommerce.php).use Jmrashed\Ecommerce\Facades\Cart;
// Add product to cart (with variant)
Cart::add([
'product_id' => $product->id,
'variant_id' => $variant->id,
'quantity' => 2,
]);
// Get cart total
$total = Cart::getTotal();
CheckoutService to validate cart, apply coupons, and process payments.$checkout = app(\Jmrashed\Ecommerce\Services\CheckoutService::class);
$order = $checkout->process([
'payment_method' => 'stripe',
'coupon_code' => 'SAVE10',
]);
config/ecommerce.php:
'payment_gateways' => [
'stripe' => [
'enabled' => true,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
'paypal' => [
'enabled' => true,
'client_id' => env('PAYPAL_CLIENT_ID'),
'secret' => env('PAYPAL_SECRET'),
],
],
$payment = app(\Jmrashed\Ecommerce\Services\PaymentService::class);
$result = $payment->charge($order->id, 'stripe');
$variant->decrement('stock', 2); // Deduct stock on order
OrderService to update statuses:
$order->updateStatus('shipped');
GET /api/products (with filters: ?category=electronics&price[min]=50).POST /api/cart (add/remove items).POST /api/checkout (process order).routes/api.php:
Route::prefix('api/ecommerce')->group(function () {
Route::resource('products', \Jmrashed\Ecommerce\Http\Controllers\Api\ProductController::class);
// Add more routes as needed
});
Modular Services:
public function __construct(
private CartService $cartService,
private OrderService $orderService
) {}
AppServiceProvider:
public function register()
{
$this->app->bind(
\Jmrashed\Ecommerce\Services\CartService::class,
\App\Services\CustomCartService::class
);
}
Event Listeners:
OrderCreated, PaymentSucceeded):
// Listen to order creation
event(new OrderCreated($order));
php artisan vendor:publish --tag="ecommerce-events"
Frontend Integration:
@ecommerceCart
@ecommerceWishlist
resources/views/vendor/ecommerce/.Testing:
$product = Product::factory()->create();
$variant = Variant::factory()->for($product)->create();
php artisan test.Payment Gateway Configuration:
config/ecommerce.php and environment variables:
STRIPE_KEY=your_key
STRIPE_SECRET=your_secret
logs/ecommerce.log for payment errors.Stock Management:
DB::transaction(function () use ($variant, $quantity) {
$variant->decrement('stock', $quantity);
// Create order
});
Variant Attribute Sync:
attribute_values as an array when creating variants:
$variant->attribute_values()->attach(['Black', 'Medium']);
Caching:
php artisan ecommerce:clear-cache
Or manually:
\Jmrashed\Ecommerce\Facades\Cache::clear();
Admin Panel Permissions:
/admin/ecommerce.app/Http/Kernel.php:
'admin' => [
\App\Http\Middleware\AdminMiddleware::class,
\Jmrashed\Ecommerce\Http\Middleware\EcommerceAdmin::class,
],
Log Everything:
config/ecommerce.php:
'debug' => env('APP_DEBUG', false),
storage/logs/ecommerce.log.Database Seeds:
php artisan migrate:fresh --seed
php artisan ecommerce:seed
API Debugging:
// Enable debugbar in AppServiceProvider
$this->app->register(\Barryvdh\Debugbar\ServiceProvider::class);
How can I help you explore Laravel packages today?