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.

class NameOfListener
{
public function handle(NameOfEvent $event)
{
//
}
}

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.

protected $listen = [
NameOfEvent::class => [
NameOfListener::class,
],
];

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

DuncanMcClean\SimpleCommerce\Events\CouponRedeemed

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

public function handle(CouponRedeemed $event)
{
$event->coupon;
}

OrderPaymentFailed

DuncanMcClean\SimpleCommerce\Events\OrderPaymentFailed

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

public function handle(OrderPaymentFailed $event)
{
$event->order;
}

OrderSaved

DuncanMcClean\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.

public function handle(OrderSaved $event)
{
$event->order;
}

OrderStatusUpdated

DuncanMcClean\SimpleCommerce\Events\OrderStatusUpdated

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

public function handle(OrderStatusUpdated $event)
{
$event->order;
$event->orderStatus;
}

PaymentStatusUpdated

DuncanMcClean\SimpleCommerce\Events\PaymentStatusUpdated

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

public function handle(PaymentStatusUpdated $event)
{
$event->order;
$event->paymentStatus;
}

PostCheckout

DuncanMcClean\SimpleCommerce\Events\PostCheckout

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

public function handle(PostCheckout $event)
{
$event->order;
$event->request;
}

PreCheckout

DuncanMcClean\SimpleCommerce\Events\PreCheckout

This event is fired before the checkout process begins.

public function handle(PreCheckout $event)
{
$event->order;
$event->request;
}

GatewayWebhookReceived

DuncanMcClean\SimpleCommerce\Events\GatewayWebhookReceived

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

public function handle(GatewayWebhookReceived $event)
{
$event->payload;
}

StockRunningLow

DuncanMcClean\SimpleCommerce\Events\StockRunningLow

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

public function handle(StockRunningLow $event)
{
$event->product;
$event->stock;
$event->variant; // If variant holds the stock
}

StockRunOut

DuncanMcClean\SimpleCommerce\Events\StockRunOut

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

public function handle(StockRunOut $event)
{
$event->product;
$event->stock;
$event->variant; // If variant holds the stock
}