sonata-project/ecommerce
Symfony-based e-commerce platform from Sonata, offering a full shopping stack with product catalog, orders, payments, shipping, customer accounts, admin backend, and integrations. Built for extensibility and customization in complex commerce projects.
Installation Require the package via Composer (note: legacy compatibility may be limited):
composer require sonata-project/ecommerce:^3.5
Run migrations (if applicable):
php artisan migrate
Bundle Setup
Register the bundle in config/app.php under SonataEcommerceBundle (if using Symfony/Laravel bridge).
Note: Ensure your Laravel app uses doctrine/dbal and doctrine/orm for Doctrine compatibility.
Basic Usage
Product model (must extend Sonata\EcommerceBundle\Model\Product).CartManager service (now compatible with Doctrine\Persistence\ManagerRegistry):
$cartManager = app('sonata.ecommerce.cart.manager');
$cartManager->add($product, $quantity);
Sonata\EcommerceBundle\Model\Order for custom logic.Admin Panel If using SonataAdmin, enable the dashboard via:
# config/packages/sonata_ecommerce.yaml
sonata_ecommerce:
dashboard:
groups:
- { label: 'Products', icon: '<i class="fa fa-cube"></i>', items: ['sonata.ecommerce.product'] }
Product Management
Sonata\EcommerceBundle\Entity\Product for custom fields.Sonata\EcommerceBundle\Entity\Category for hierarchical taxonomies.Product::getStock() and update with:
$product->setStock($product->getStock() - $quantity);
$product->getManager()->flush(); // Explicit flush due to Doctrine changes
Shopping Cart
config/packages/sonata_ecommerce.yaml).Sonata\EcommerceBundle\Manager\CartManager to add discounts or validation:
public function add(Product $product, $quantity) {
if (!$this->isProductAvailable($product, $quantity)) {
throw new \RuntimeException('Out of stock');
}
parent::add($product, $quantity);
}
Checkout Process
OrderManager to create orders:
$orderManager = app('sonata.ecommerce.order.manager');
$order = $orderManager->create($cart, $customer);
sonata.ecommerce.order.post_create event.Discounts & Promotions
Sonata\EcommerceBundle\Model\DiscountInterface for custom rules.$discount = $this->getDiscountService()->apply($cart);
$cart->setDiscount($discount);
$cart->getManager()->flush(); // Explicit flush
API/Headless Use
return type resolvers (ensure Doctrine entities are properly mapped).Doctrine\Persistence\ManagerRegistry in config/services.php:
'sonata.ecommerce.manager_registry' => \Doctrine\Persistence\ManagerRegistry::class,
Event system for order lifecycle hooks:
event(new OrderCreated($order));
/cart/update).CartManager and OrderManager in PHPUnit:
$this->mock(CartManager::class)->shouldReceive('add')->once();
Doctrine Registry Change
Symfony\Bridge\Doctrine\RegistryInterface replaced with Doctrine\Persistence\ManagerRegistry.
Fix: Update service bindings and ensure your Laravel app uses the new registry:
$registry = app('doctrine')->getManagerRegistry();
Abandoned Package Risks
bagisto or laravel-shoppingcart.Session Cart Issues
CartManager::clear() in logout middleware.Database Schema
sonata_ecommerce_product may not align with Laravel conventions.
Fix: Create custom migrations or use Doctrine migrations via doctrine/dbal.Admin Panel Quirks
laravel-bridge.Cart Not Updating
sonata.ecommerce.cart.session is configured in config/packages/sonata_ecommerce.yaml..env (SESSION_DRIVER=file for testing).ManagerRegistry is properly injected:
$this->registry = app('doctrine')->getManagerRegistry();
Product Not Saving
Product extends Sonata\EcommerceBundle\Model\Product and annotations are present.$errors = $validator->validate($product);
$product->getManager()->flush();
Order Not Creating
OrderManager dependencies (e.g., Customer, PaymentMethod).APP_DEBUG=true) to log missing services.Event Listeners Failing
EventServiceProvider:
protected $listen = [
'sonata.ecommerce.order.post_create' => [
\App\Listeners\OrderCreatedListener::class,
],
];
Custom Fields
Product via Doctrine extensions or Laravel accessors:
public function getFormattedPrice() {
return '$' . $this->price;
}
Payment Gateways
Sonata\EcommerceBundle\Model\PaymentMethod or create a wrapper service:
class StripePaymentGateway {
public function charge(Order $order) {
$entityManager = $order->getManager();
// Integrate with Stripe API
}
}
Shipping Methods
Sonata\EcommerceBundle\Model\ShippingMethodInterface for custom logic.Frontend Templates
resources/views/vendor/sonata_ecommerce/ (if using Blade).templates/SonataEcommerceBundle/.Testing Utilities
trait EcommerceTestTrait {
protected function createProduct() {
$product = Product::factory()->create();
$product->setManager(app('doctrine')->getManager());
return $product;
}
}
How can I help you explore Laravel packages today?