Skip to content

Commit

Permalink
Populate customer_id on Cart if available. (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson authored May 1, 2024
1 parent da70d1e commit fde78db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core/src/Actions/Carts/AssociateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function execute(Cart $cart, User $user, $policy = 'merge'): self

$cart->update([
'user_id' => $user->getKey(),
'customer_id' => $user->latestCustomer()?->getKey(),
]);

return $this;
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/Managers/CartSessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,13 @@ public function createOrder($forget = true)
*/
protected function createNewCart()
{
$user = $this->authManager->user();

$cart = Cart::create([
'currency_id' => $this->getCurrency()->id,
'channel_id' => $this->getChannel()->id,
'user_id' => $this->authManager->user()?->id,
'user_id' => optional($user)->id,
'customer_id' => optional($user)->latestCustomer()?->id,
]);

return $this->use($cart);
Expand Down
29 changes: 29 additions & 0 deletions tests/core/Unit/Actions/Carts/AssociateUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@
]);
});

test('can associate a user with a customer', function () {
$currency = Currency::factory()->create();

$cart = Cart::factory()->create([
'currency_id' => $currency->id,
]);

$this->assertDatabaseHas((new Cart)->getTable(), [
'user_id' => null,
'id' => $cart->id,
'merged_id' => null,
]);

$action = new AssociateUser;

$user = User::factory()->create();
$customer = \Lunar\Models\Customer::factory()->create();
$user->customers()->attach($customer);

$action->execute($cart, $user);

$this->assertDatabaseHas((new Cart)->getTable(), [
'user_id' => $user->id,
'customer_id' => $customer->id,
'id' => $cart->id,
'merged_id' => null,
]);
});

test('cant associate user to cart with order', function () {
$currency = Currency::factory()->create();

Expand Down

0 comments on commit fde78db

Please sign in to comment.