Skip to content

Commit f1f40c6

Browse files
Add always_checkout_as_guest config option (#170)
1 parent 8b2b0b8 commit f1f40c6

4 files changed

Lines changed: 112 additions & 1 deletion

File tree

config/cargo.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@
106106

107107
'merge_on_login' => true,
108108

109+
/*
110+
|--------------------------------------------------------------------------
111+
| Always checkout as guest
112+
|--------------------------------------------------------------------------
113+
|
114+
| By default, when a logged-in user checks out, Cargo will associate the order
115+
| with their user account. Enable this option to always create guest customers
116+
| instead, even when users are logged in.
117+
|
118+
*/
119+
120+
'always_checkout_as_guest' => false,
121+
109122
/*
110123
|--------------------------------------------------------------------------
111124
| Cookie Name

docs/docs/customers.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,18 @@ You can disable this behaviour in the `cargo.php` config file:
3333
'merge_on_login' => true,
3434
],
3535
```
36+
37+
## Always checkout as guest
38+
By default, when a logged-in user checks out, Cargo will associate the order with their user account.
39+
40+
If you'd prefer to always create guest customers instead (even when users are logged in), you may enable the `always_checkout_as_guest` option:
41+
42+
```php
43+
// config/statamic/cargo.php
44+
45+
'carts' => [
46+
'always_checkout_as_guest' => true,
47+
],
48+
```
49+
50+
When enabled, customer details will be saved on the order, rather than being linked to a user account.

src/Http/Controllers/Concerns/HandlesCustomerInformation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function handleCustomerInformation(Request $request, Cart $cart): Cart
1818
'email' => $request->email,
1919
])->filter()->all());
2020

21-
if ($user = User::current()) {
21+
if (! config('statamic.cargo.carts.always_checkout_as_guest') && $user = User::current()) {
2222
// When a customer is missing from the order, we'll set it to the logged-in user.
2323
if (! $cart->customer()) {
2424
$cart->customer($user);

tests/Cart/CartControllerTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPUnit\Framework\Attributes\Test;
1010
use Statamic\Facades\Collection;
1111
use Statamic\Facades\Entry;
12+
use Statamic\Facades\User;
1213
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
1314
use Tests\TestCase;
1415

@@ -198,6 +199,88 @@ public function it_throws_validation_error_when_customer_email_is_invalid()
198199
$this->assertNull($cart->fresh()->customer());
199200
}
200201

202+
#[Test]
203+
public function it_assigns_logged_in_user_to_cart_by_default()
204+
{
205+
config()->set('statamic.cargo.carts.always_checkout_as_guest', false);
206+
207+
$cart = $this->makeCart();
208+
$cart->customer(null)->save();
209+
210+
$user = User::make()->email('user@example.com')->save();
211+
212+
$this->actingAs($user)
213+
->from('/cart')
214+
->patch('/!/cargo/cart', [
215+
'customer' => [
216+
'name' => 'John Doe',
217+
'email' => 'john@example.com',
218+
],
219+
])
220+
->assertRedirect('/cart');
221+
222+
$cart = $cart->fresh();
223+
224+
$this->assertNotInstanceOf(GuestCustomer::class, $cart->customer());
225+
$this->assertEquals($user->id(), $cart->customer()->id());
226+
}
227+
228+
#[Test]
229+
public function it_creates_guest_customer_when_always_checkout_as_guest_is_enabled()
230+
{
231+
config()->set('statamic.cargo.carts.always_checkout_as_guest', true);
232+
233+
$cart = $this->makeCart();
234+
$cart->customer(null)->save();
235+
236+
$user = User::make()->email('user@example.com')->save();
237+
238+
$this->actingAs($user)
239+
->from('/cart')
240+
->patch('/!/cargo/cart', [
241+
'customer' => [
242+
'name' => 'John Doe',
243+
'email' => 'john@example.com',
244+
],
245+
])
246+
->assertRedirect('/cart');
247+
248+
$cart = $cart->fresh();
249+
250+
$this->assertInstanceOf(GuestCustomer::class, $cart->customer());
251+
$this->assertEquals('John Doe', $cart->customer()->name());
252+
$this->assertEquals('john@example.com', $cart->customer()->email());
253+
}
254+
255+
#[Test]
256+
public function it_does_not_update_user_data_when_always_checkout_as_guest_is_enabled()
257+
{
258+
config()->set('statamic.cargo.carts.always_checkout_as_guest', true);
259+
260+
$cart = $this->makeCart();
261+
$cart->customer(null)->save();
262+
263+
$user = User::make()
264+
->email('original@example.com')
265+
->set('name', 'Original Name')
266+
->save();
267+
268+
$this->actingAs($user)
269+
->from('/cart')
270+
->patch('/!/cargo/cart', [
271+
'customer' => [
272+
'name' => 'New Name',
273+
'email' => 'new@example.com',
274+
],
275+
])
276+
->assertRedirect('/cart');
277+
278+
$user = User::find($user->id());
279+
280+
$this->assertEquals('original@example.com', $user->email());
281+
$this->assertEquals('Original Name', $user->get('name'));
282+
}
283+
201284
#[Test]
202285
public function it_deletes_the_cart()
203286
{

0 commit comments

Comments
 (0)