Carts & Orders
Email Notifications
When you install Cargo, it will automatically create two mailables for you, resulting in classes in app/Mail and views in resources/views/emails:
| Mailable | Sent when... |
|---|---|
App\Mail\OrderConfirmation |
An order's payment has been received. |
App\Mail\OrderShipped |
An order has been marked as shipped. If a tracking number has been provided, it'll be included in the email. |
Cargo will also configure event listeners in your AppServiceProvider, triggering the emails to send whenever Cargo's OrderPaymentReceived and OrderShipped events are dispatched.
// app/Providers/AppServiceProvider.php
use App\Mail\OrderConfirmation;
use App\Mail\OrderShipped;
use DuncanMcClean\Cargo\Events\OrderPaymentReceived;
use DuncanMcClean\Cargo\Events\OrderShipped as OrderShippedEvent;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
Event::listen(OrderPaymentReceived::class, function ($event) {
Mail::to($event->order->customer())
->locale($event->order->site()->shortLocale())
->send(new OrderConfirmation($event->order));
});
Event::listen(OrderShippedEvent::class, function ($event) {
Mail::to($event->order->customer())
->locale($event->order->site()->shortLocale())
->send(new OrderShipped($event->order));
});
To change the content of the emails, all you need to do is edit the views or mailable classes in your app.
If you installed Cargo before the
OrderShippedmailable was introduced, you can runphp please cargo:installagain to publish it. Any mailables you've already published will be left untouched.
Sending your own emails
If you want to send any other emails, you can create your own mailable, then configure an event listener to trigger it based on one of Cargo's events:
php artisan make:mail OrderCancelled
// app/Providers/AppServiceProvider.php
use App\Mail\OrderCancelled;
use DuncanMcClean\Cargo\Events\OrderCancelled as OrderCancelledEvent;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Mail;
Event::listen(OrderCancelledEvent::class, function ($event) {
Mail::to($event->order->customer())
->locale($event->order->site()->shortLocale())
->send(new OrderCancelled($event->order));
});
For more information on sending emails, please consult the Laravel documentation.
Previewing emails in the browser
You can preview Mailables by returning them from a route, like this:
// routes/web.php
Route::get('/order-confirmation', function () {
$order = Order::query()->orderByDesc('date')->first();
return new OrderConfirmation($order);
});
Route::get('/order-shipped', function () {
$order = Order::query()->orderByDesc('date')->first();
return new OrderShipped($order);
});
You may want to wrap the routes in a if (! app()->isProduction()) conditional to ensure the emails aren't accessible in production.