Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anon160124's avatar

Fix ecommerce Listeners\CartUpdatedListener error

Hi, Could you help me fix the error I'm getting?

CartUpdatedListener.php

    public function handle($event)
    {
        $couponName = session()->get('coupon')['name'];

        if ($couponName) {
            $coupon = Coupon::where('code', $couponName)->first();

            dispatch_now(new UpdateCoupon($coupon));
        }
    }

Execute the job

public function handle()
    {
        if (Cart::currentInstance() === 'default') {
            session()->put('coupon', [
                'name' => $this->coupon->code,
                'discount' => $this->coupon->discount(Cart::subtotal()),
            ]);
        }
    }

ERROR: Trying to access array offset on value of type null

0 likes
2 replies
MichalOravec's avatar
Level 75

You session coupon doesn't exists

public function handle($event)
{
    $couponName = session()->get('coupon')['name'] ?? null;

    if ($couponName) {
        $coupon = Coupon::where('code', $couponName)->first();

        dispatch_now(new UpdateCoupon($coupon));
    }
}

Please or to participate in this conversation.