Skip to content

Commit

Permalink
Chore - Merge 0.8 into 1.0 (#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson authored Mar 5, 2024
1 parent d90421c commit 9cc76c4
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 40 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_size = 4
indent_style = space
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
* text=auto eol=lf

*.blade.php diff=html
*.css diff=css
*.html diff=html
*.md diff=markdown
*.php diff=php

/.github export-ignore
/docs export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/monorepo-builder.php export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml export-ignore
/pint.json export-ignore
20 changes: 11 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/vendor/
composer.lock
/vendor
.phpunit.result.cache
.phpunit.cache/
/.DS_Store
/.fleet
/.idea
/.nova
/.phpunit.cache
/.vscode
node_modules/
yarn.lock
/node_modules
/vendor

.phpunit.result.cache
composer.lock
package-lock.json
.DS_Store
.nova
phpstan.neon
Thumbs.db
yarn.lock
32 changes: 31 additions & 1 deletion docs/core/reference/carts.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ CartSession::add($purchasable, $quantity);
```php
CartSession::addLines([
[
'id' => 1,
'purchasable' => \Lunar\Models\ProductVariant::find(123),
'quantity' => 25,
'meta' => ['foo' => 'bar'],
],
Expand Down Expand Up @@ -466,3 +466,33 @@ return [
```

In most cases you won't need to change this.

## Pruning cart data

Over time you will experience a build up of carts in your database that you may want to regularly remove.

You can enable automatic removal of these carts using the `lunar.carts.prune_tables.enabled` config. By setting this to `true` any carts without an order associated will be removed after 90 days.

You can change the number of days carts are retained for using the `lunar.carts.prune_tables. prune_interval` config.

If you have specific needs around pruning you can also change the `lunar.carts.prune_tables.pipelines` array to determine what carts should be removed.



```php
return [
// ...
'prune_tables' => [

'enabled' => false,

'pipelines' => [
Lunar\Pipelines\CartPrune\PruneAfter::class,
Lunar\Pipelines\CartPrune\WithoutOrders::class,
],

'prune_interval' => 90, // days

],
];
```
8 changes: 8 additions & 0 deletions docs/core/reference/discounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ Discount::usable()->get();
Discount::products($productIds, $type = 'condition');
```

### Resetting the discount cache

For performance reasons the applicable discounts are cached per request. If you need to reset this cache (for example after adding a discount code) you should call `resetDiscounts()`:

```php
Discount::resetDiscounts();
```

## Discount Purchasable

You can relate a purchasable to a discount via this model. Each has a type for whether it's a `condition` or `reward`.
Expand Down
5 changes: 5 additions & 0 deletions docs/core/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ php artisan migrate

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


## 1.0

### High Impact
Expand Down Expand Up @@ -58,6 +59,10 @@ public Collection $tiered,
public Collection $priceBreaks,
```

## 0.8

No significant changes.

## 0.7

### High Impact
Expand Down
23 changes: 23 additions & 0 deletions packages/core/config/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,27 @@
'lines.purchasable.product',
'lines.cart.currency',
],

/*
|--------------------------------------------------------------------------
| Prune carts
|--------------------------------------------------------------------------
|
| Should the cart models be pruned to prevent data build up and
| some settings controlling how pruning should be determined
|
*/
'prune_tables' => [

'enabled' => false,

'pipelines' => [
Lunar\Pipelines\CartPrune\PruneAfter::class,
Lunar\Pipelines\CartPrune\WithoutOrders::class,
Lunar\Pipelines\CartPrune\WhereNotMerged::class,
],

'prune_interval' => 90, // days

],
];
1 change: 0 additions & 1 deletion packages/core/database/state/ConvertTaxbreakdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public function run()
});
});
}

});
}

Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/Base/Casts/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public function get($model, $key, $value, $attributes)
{
$currency = $model->currency ?: Currency::getDefault();

/**
* Make it an integer based on currency requirements.
*/
$value = preg_replace('/[^0-9]/', '', $value);
if (! is_null($value)) {
/**
* Make it an integer based on currency requirements.
*/
$value = preg_replace('/[^0-9]/', '', $value);
}

Validator::make([
$key => $value,
Expand Down
25 changes: 23 additions & 2 deletions packages/core/src/Base/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Lunar\Base\Traits;

use Illuminate\Support\Arr;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity as SpatieLogsActivity;

trait LogsActivity
{
use SpatieLogsActivity;

public static array $logExcept = [];

/**
* Get the log options for the activity log.
*/
Expand All @@ -17,7 +20,25 @@ public function getActivitylogOptions(): LogOptions
return LogOptions::defaults()
->useLogName('lunar')
->logAll()
->dontSubmitEmptyLogs()
->logExcept(['updated_at']);
->logExcept(array_merge(['updated_at'], static::getActivitylogExcept()))
->logOnlyDirty()
->dontSubmitEmptyLogs();
}

public static function addActivitylogExcept(array|string $fields)
{
$fields = Arr::wrap($fields);

static::$logExcept = array_merge(static::$logExcept, $fields);
}

public static function getDefaultLogExcept(): array
{
return [];
}

public static function getActivitylogExcept(): array
{
return array_merge(static::getDefaultLogExcept(), static::$logExcept);
}
}
57 changes: 57 additions & 0 deletions packages/core/src/Console/Commands/PruneCarts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Lunar\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Pipeline\Pipeline;
use Lunar\Models\Cart;

class PruneCarts extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lunar:prune:carts';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Prune the carts table';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->info('Beginning prune');

$query = Cart::query();

$carts = app(Pipeline::class)
->send($query)
->through(
config('lunar.cart.prune_tables.pipelines', [])
)->then(function ($query) {
$query->chunk(200, function ($carts) {
$carts->each(fn ($cart) => $this->pruneCart($cart));
});
});

$this->info('Prune complete');
}

public function pruneCart(Cart $cart)
{
Cart::where('merged_id', $cart->id)->get()->each(fn ($merged) => $this->pruneCart($merged));

$cart->lines()->delete();
$cart->addresses()->delete();
$cart->delete();
}
}
9 changes: 9 additions & 0 deletions packages/core/src/LunarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cartalyst\Converter\Laravel\Facades\Converter;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Logout;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Database\Events\MigrationsStarted;
use Illuminate\Database\Events\NoPendingMigrations;
Expand Down Expand Up @@ -39,6 +40,7 @@
use Lunar\Console\Commands\Import\AddressData;
use Lunar\Console\Commands\MigrateGetCandy;
use Lunar\Console\Commands\Orders\SyncNewCustomerOrders;
use Lunar\Console\Commands\PruneCarts;
use Lunar\Console\Commands\ScoutIndexerCommand;
use Lunar\Console\InstallLunar;
use Lunar\Database\State\ConvertProductTypeAttributesToProducts;
Expand Down Expand Up @@ -210,7 +212,14 @@ public function boot(): void
ScoutIndexerCommand::class,
MigrateGetCandy::class,
SyncNewCustomerOrders::class,
PruneCarts::class,
]);

if (config('lunar.cart.prune_tables.enabled', false)) {
$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
$schedule->command('lunar:prune:carts')->daily();
});
}
}

Arr::macro('permutate', [\Lunar\Utils\Arr::class, 'permutate']);
Expand Down
24 changes: 20 additions & 4 deletions packages/core/src/Managers/DiscountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,27 @@ public function getDiscounts(Cart $cart = null): Collection
function ($query, $value) {
return $query->where(function ($query) use ($value) {
return $query->where(fn ($query) => $query->products(
$value->lines->pluck('purchasable.product_id')->filter()->values()
$value->lines->pluck('purchasable.product_id')->filter()->values(),
['condition', 'limitation']
)
)
->orWhere(fn ($query) => $query->productVariants(
$value->lines->pluck('purchasable.id')->filter()->values()
$value->lines->pluck('purchasable.id')->filter()->values(),
['condition', 'limitation']
)
);
});
}
)->when(
)
->when(
$cart?->coupon_code,
fn ($query, $value) => $query->where('coupon', '=', $value)->orWhere(fn ($query) => $query->whereNull('coupon')->orWhere('coupon', '')),
function ($query, $value) {
return $query->where(function ($query) use ($value) {
$query->where('coupon', $value)
->orWhereNull('coupon')
->orWhere('coupon', '');
});
},
fn ($query, $value) => $query->whereNull('coupon')->orWhere('coupon', '')
)->orderBy('priority', 'desc')
->orderBy('id')
Expand Down Expand Up @@ -203,6 +212,13 @@ public function apply(Cart $cart): Cart
return $cart;
}

public function resetDiscounts(): self
{
$this->discounts = null;

return $this;
}

public function validateCoupon(string $coupon): bool
{
return app(
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/Managers/StorefrontSessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function initCustomerGroups()
);

if ($this->customerGroups?->count()) {
if (! $groupHandles) {
if ($groupHandles->isEmpty()) {
return $this->setCustomerGroups(
$this->customerGroups
);
Expand All @@ -89,16 +89,14 @@ public function initCustomerGroups()
return $this->customerGroups;
}

if (! $this->customerGroups?->count()) {
return $this->setCustomerGroups(
collect([
CustomerGroup::getDefault(),
])
);
if (! $groupHandles->isEmpty()) {
return $this->customerGroups = CustomerGroup::whereIn('handle', $groupHandles)->get();
}

return $this->setCustomerGroups(
CustomerGroup::whereIn('handle', $groupHandles)->get()
collect([
CustomerGroup::getDefault(),
])
);
}

Expand Down
Loading

0 comments on commit 9cc76c4

Please sign in to comment.