Skip to content

Commit

Permalink
Add cart soft deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed May 23, 2024
1 parent fe6108a commit 17e67ed
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/core/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ php artisan migrate

Lunar currently provides bug fixes and security updates for only the latest minor release, e.g. `0.7`.

## 1.0.0-alpha.22

### Medium Impact

Carts now use soft deletes and a cart will be deleted when `CartSession::forget()` is called.
If you don't want to delete the cart when you call `forget` you can pass `delete: false` as a parameter:

```php
\Lunar\Facades\CartSession::forget(delete: false);
```

## 1.0.0-alpha.20

### High Impact
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Lunar\Base\Migration;

return new class extends Migration
{
public function up(): void
{
Schema::table($this->prefix.'carts', function (Blueprint $table) {
$table->softDeletes();
});
}

public function down(): void
{
Schema::table($this->prefix.'carts', function (Blueprint $table) {
$table->dropColumn('deleted_at');
});
}
};
11 changes: 10 additions & 1 deletion packages/core/src/Managers/CartSessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ public function getShippingEstimateMeta(): array
/**
* {@inheritDoc}
*/
public function forget()
public function forget(bool $delete = true)
{
if ($delete) {
Cart::destroy(
$this->sessionManager->get(
$this->getSessionKey()
)
);
}

$this->sessionManager->forget('shipping_estimate_meta');
$this->sessionManager->forget(
$this->getSessionKey()
);

}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -53,13 +54,15 @@
* @property ?\Illuminate\Support\Carbon $completed_at
* @property ?\Illuminate\Support\Carbon $created_at
* @property ?\Illuminate\Support\Carbon $updated_at
* @property ?\Illuminate\Support\Carbon $deleted_at
*/
class Cart extends BaseModel
{
use CachesProperties;
use HasFactory;
use HasMacros;
use LogsActivity;
use SoftDeletes;

/**
* Array of cachable class properties.
Expand Down
91 changes: 91 additions & 0 deletions tests/core/Unit/Managers/CartSessionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,97 @@

});

test('can forget a cart and soft delete it', function () {
Currency::factory()->create([
'default' => true,
]);

Channel::factory()->create([
'default' => true,
]);

Config::set('lunar.cart.auto_create', true);

$cart = CartSession::current();

$shipping = CartAddress::factory()->create([
'cart_id' => $cart->id,
'type' => 'shipping',
]);

$billing = CartAddress::factory()->create([
'cart_id' => $cart->id,
'type' => 'billing',
]);

$cart->setShippingAddress($shipping);
$cart->setBillingAddress($billing);

$sessionCart = Session::get(config('lunar.cart.session_key'));

expect($sessionCart)
->not
->toBeNull()
->and($sessionCart)
->toEqual($cart->id);

CartSession::forget();

expect(
Session::get(config('lunar.cart.session_key'))
)
->toBeNull()
->and($cart->refresh()->deleted_at)
->not
->toBeNull();

});

test('can forget a cart an optionally prevent soft deleting', function () {
Currency::factory()->create([
'default' => true,
]);

Channel::factory()->create([
'default' => true,
]);

Config::set('lunar.cart.auto_create', true);

$cart = CartSession::current();

$shipping = CartAddress::factory()->create([
'cart_id' => $cart->id,
'type' => 'shipping',
]);

$billing = CartAddress::factory()->create([
'cart_id' => $cart->id,
'type' => 'billing',
]);

$cart->setShippingAddress($shipping);
$cart->setBillingAddress($billing);

$sessionCart = Session::get(config('lunar.cart.session_key'));

expect($sessionCart)
->not
->toBeNull()
->and($sessionCart)
->toEqual($cart->id);

CartSession::forget(delete: false);

expect(
Session::get(config('lunar.cart.session_key'))
)
->toBeNull()
->and($cart->refresh()->deleted_at)
->toBeNull();

});

test('can set shipping estimate meta', function () {
CartSession::estimateShippingUsing([
'postcode' => 'NP1 1TX',
Expand Down

0 comments on commit 17e67ed

Please sign in to comment.