Skip to content

Commit e960d05

Browse files
committed
Fix "Recursion detected" error with cart endpoints
1 parent 48c8290 commit e960d05

5 files changed

+47
-9
lines changed

src/Http/Controllers/BaseActionController.php

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ class BaseActionController extends Controller
1010
{
1111
protected function withSuccess(Request $request, array $data = [])
1212
{
13+
// The cart is only useful in a JSON response, so we'll remove it from
14+
// the $data array before we pass it to the view.
15+
if (Arr::has($data, 'cart')) {
16+
// dd($data['cart']);
17+
// unset($data['cart']['customer']);
18+
// unset($data['cart']['customer_id']);
19+
20+
// dd($data['cart']['customer']);
21+
}
22+
1323
if ($request->wantsJson()) {
1424
$data = array_merge($data, [
1525
'status' => 'success',

src/Http/Controllers/CartController.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public function index(IndexRequest $request)
2222
}
2323

2424
return [
25-
'data' => $this->getCart()
25+
'cart' => $this->getCart()
2626
->toAugmentedCollection()
27+
->withRelations(['customer', 'customer_id'])
2728
->withShallowNesting()
2829
->toArray(),
2930
];
@@ -58,7 +59,11 @@ public function update(UpdateRequest $request)
5859

5960
return $this->withSuccess($request, [
6061
'message' => __('Cart Updated'),
61-
'cart' => $cart->toAugmentedArray(),
62+
'cart' => $this->getCart()
63+
->toAugmentedCollection()
64+
->withRelations(['customer', 'customer_id'])
65+
->withShallowNesting()
66+
->toArray(),
6267
]);
6368
}
6469

@@ -72,7 +77,6 @@ public function destroy(DestroyRequest $request)
7277

7378
return $this->withSuccess($request, [
7479
'message' => __('Cart Deleted'),
75-
'cart' => null,
7680
]);
7781
}
7882

src/Http/Controllers/CartItemController.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ public function store(StoreRequest $request)
119119

120120
return $this->withSuccess($request, [
121121
'message' => __('Added to Cart'),
122-
'cart' => $cart->fresh()->toAugmentedArray(),
122+
'cart' => $this->getCart()
123+
->toAugmentedCollection()
124+
->withRelations(['customer', 'customer_id'])
125+
->withShallowNesting()
126+
->toArray(),
123127
]);
124128
}
125129

@@ -163,7 +167,11 @@ public function update(UpdateRequest $request, string $requestItem)
163167

164168
return $this->withSuccess($request, [
165169
'message' => __('Line Item Updated'),
166-
'cart' => $cart->toAugmentedArray(),
170+
'cart' => $this->getCart()
171+
->toAugmentedCollection()
172+
->withRelations(['customer', 'customer_id'])
173+
->withShallowNesting()
174+
->toArray(),
167175
]);
168176
}
169177

@@ -175,7 +183,11 @@ public function destroy(DestroyRequest $request, string $item)
175183

176184
return $this->withSuccess($request, [
177185
'message' => __('Item Removed from Cart'),
178-
'cart' => $cart->toAugmentedArray(),
186+
'cart' => $this->getCart()
187+
->toAugmentedCollection()
188+
->withRelations(['customer', 'customer_id'])
189+
->withShallowNesting()
190+
->toArray(),
179191
]);
180192
}
181193

src/Http/Controllers/CheckoutController.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public function __invoke(CheckoutRequest $request)
5252

5353
return $this->withSuccess($request, [
5454
'message' => __('Checkout Complete!'),
55-
'cart' => $this->order->toAugmentedArray(),
55+
'cart' => $this->getCart()
56+
->toAugmentedCollection()
57+
->withRelations(['customer', 'customer_id'])
58+
->withShallowNesting()
59+
->toArray(),
5660
'is_checkout_request' => true,
5761
]);
5862
}

src/Http/Controllers/CouponController.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public function store(StoreRequest $request)
2525

2626
return $this->withSuccess($request, [
2727
'message' => __('Coupon added to cart'),
28-
'cart' => $this->getCart()->toAugmentedArray(),
28+
'cart' => $this->getCart()
29+
->toAugmentedCollection()
30+
->withRelations(['customer', 'customer_id'])
31+
->withShallowNesting()
32+
->toArray(),
2933
]);
3034
}
3135

@@ -41,7 +45,11 @@ public function destroy(DestroyRequest $request)
4145

4246
return $this->withSuccess($request, [
4347
'message' => __('Coupon removed from cart'),
44-
'cart' => $this->getCart()->toAugmentedArray(),
48+
'cart' => $this->getCart()
49+
->toAugmentedCollection()
50+
->withRelations(['customer', 'customer_id'])
51+
->withShallowNesting()
52+
->toArray(),
4553
]);
4654
}
4755
}

0 commit comments

Comments
 (0)