You're viewing the documentation of an older Simple Commerce version. Consider upgrading to the latest for new features and bug fixes.

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

CartCompleted

DoubleThreeDigital\SimpleCommerce\Events\CartCompleted

This event is fired when an order/cart is complete and has been paid, usually after checking out.

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

CartSaved

DoubleThreeDigital\SimpleCommerce\Events\CartSaved

This event is fired when an order/cart has been saved.

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

CartUpdated

DoubleThreeDigital\SimpleCommerce\Events\CartUpdated

I'd recommend you use the CartSaved event instead. The CartUpdated will be deprecated in future versions.

This event is fired when an order/cart has been saved.

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

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}

CustomerAddedToCart

DoubleThreeDigital\SimpleCommerce\Events\CustomerAddedToCart

This event is fired when a customer is attached to a cart/order.

1public function handle(CustomerAddedToCart $event)
2{
3 $event->cart;
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->data;
4}

PreCheckout

DoubleThreeDigital\SimpleCommerce\Events\PreCheckout

This event is fired before the checkout process begins.

1public function handle(PreCheckout $event)
2{
3 $event->data;
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}
Last updated 2 months ago Improve this page on GitHub!