Simple Commerce Logo

Events

Events can be useful if you need to listen out for when certain things in your application happen.

Simple Commerce provides a couple of events to help make your life easier.

Listening for events

First, you'll need a Listener class. You can generate one by running php artisan make:listener NameOfListener. It'll generate a file in App\Listeners.

In the handle function of your event listener, that's where you write your logic that you need to happen when a certain event is triggered.

1class NameOfListener
2{
3 public function handle(NameOfEvent $event)
4 {
5 //
6 }
7}

Before your listener will actually listen to an event, you need to hook it up. You can do this in your EventServiceProvider, located in App\Providers.

1protected $listen = [
2 NameOfEvent::class => [
3 NameOfListener::class,
4 ],
5];

And there you go, you have a listener listening to an event!

For more documentation around events and event listeners, consider reading the Laravel documentation.

Available events

CouponRedeemed

DoubleThreeDigital\SimpleCommerce\Events\CouponRedeemed

This event is fired when a customer adds a coupon to their cart/order.

1public function handle(CouponRedeemed $event)
2{
3 $event->coupon;
4}

OrderPaid

DoubleThreeDigital\SimpleCommerce\Events\OrderPaid

This event is fired when an order has been marked as paid.

1public function handle(OrderPaid $event)
2{
3 $event->order;
4}

OrderSaved

DoubleThreeDigital\SimpleCommerce\Events\OrderSaved

This event is fired when an order has been saved. This event will only be fired when an order is saved via Simple Commerce, not via the Control Panel.

1public function handle(OrderPaid $event)
2{
3 $event->order;
4}

PostCheckout

DoubleThreeDigital\SimpleCommerce\Events\PostCheckout

This event is fired after the checkout process has been completed.

1public function handle(PostCheckout $event)
2{
3 $event->order;
4}

PreCheckout

DoubleThreeDigital\SimpleCommerce\Events\PreCheckout

This event is fired before the checkout process begins.

1public function handle(PreCheckout $event)
2{
3 $event->order;
4}

ReceiveGatewayWebhook

DoubleThreeDigital\SimpleCommerce\Events\ReceiveGatewayWebhook

This event is fired whenever a webhook request from a gateway is received.

1public function handle(ReceiveGatewayWebhook $event)
2{
3 $event->payload;
4}

StockRunOut

DoubleThreeDigital\SimpleCommerce\Events\StockRunOut

This event is fired when the stock for a product has ran out.

1public function handle(StockRunOut $event)
2{
3 $event->product;
4 $event->stock;
5}

StockRunningLow

DoubleThreeDigital\SimpleCommerce\Events\StockRunningLow

This event is fired when the stock for a product is running low.

1public function handle(StockRunningLow $event)
2{
3 $event->product;
4 $event->stock;
5}

Need some help?

There's plenty of ways of getting help: either via opening a GitHub Issue, starting a conversation on the Statamic Discord or directly emailing me.