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