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}

OrderPaymentFailed

DoubleThreeDigital\SimpleCommerce\Events\OrderPaymentFailed

This event is fired whenever a payment fails to go through.

1public function handle(OrderPaymentFailed $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(OrderSaved $event)
2{
3 $event->order;
4}

OrderStatusUpdated

DoubleThreeDigital\SimpleCommerce\Events\OrderStatusUpdated

This event is fired whenever the status of an order changes.

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

PaymentStatusUpdated

DoubleThreeDigital\SimpleCommerce\Events\PaymentStatusUpdated

This event is fired whenever the payment status of an order changes.

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

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 $event->request;
5}

PreCheckout

DoubleThreeDigital\SimpleCommerce\Events\PreCheckout

This event is fired before the checkout process begins.

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

GatewayWebhookReceived

DoubleThreeDigital\SimpleCommerce\Events\GatewayWebhookReceived

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

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

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 $event->variant; // If variant holds the stock
6}

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 $event->variant; // If variant holds the stock
6}