Skip to content

Commit

Permalink
Merge branch '0.8' into 0.7_0.8_branch_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
glennjacobs authored Jan 31, 2024
2 parents 65a84ac + 0cb0168 commit 79b603d
Show file tree
Hide file tree
Showing 22 changed files with 316 additions and 30 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
19 changes: 19 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
* 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
/packages/**/tests export-ignore
/utils/**/tests 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
19 changes: 11 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/vendor/
composer.lock
/vendor
.phpunit.result.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
30 changes: 30 additions & 0 deletions docs/core/reference/carts.md
Original file line number Diff line number Diff line change
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
7 changes: 4 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"type": "module",
"devDependencies": {
"vitepress": "^1.0.0-beta.7"
"vitepress": "^1.0.0-rc.31"
},
"scripts": {
"docs:dev": "vitepress dev",
"docs:build": "vitepress build",
"docs:preview": "vitepress preview"
}
}
},
"type": "module"
}
7 changes: 7 additions & 0 deletions packages/admin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text=auto eol=lf

/.github export-ignore
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml export-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class ActivityLogFeed extends Component
* The new comment for the subject.
*/
public ?string $comment = null;


protected $listeners = ['activityUpdated' => '$refresh'];

/**
* {@inheritDoc}
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/core/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* text=auto eol=lf

/.github export-ignore
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml export-ignore
22 changes: 22 additions & 0 deletions packages/core/config/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,26 @@
'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,
],

'prune_interval' => 90, // days

],
];
2 changes: 0 additions & 2 deletions packages/core/database/state/ConvertTaxbreakdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public function prepare()
public function run()
{
DB::usingConnection(config('lunar.database.connection') ?: DB::getDefaultConnection(), function () {

$prefix = config('lunar.database.table_prefix');
$updateTime = now();

Expand Down Expand Up @@ -74,7 +73,6 @@ public function run()
});
});
}

});
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/Base/StandardMediaConversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Lunar\Base;

use Illuminate\Database\Eloquent\Model;
use Spatie\Image\Manipulations;

class StandardMediaConversions
{
public function apply(BaseModel $model)
public function apply(Model $model)
{
$conversions = [
'zoom' => [
Expand Down
54 changes: 54 additions & 0 deletions packages/core/src/Console/Commands/PruneCarts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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(function ($cart) {
Cart::where('merged_id', $cart->id)->update(['merged_id' => null]);

$cart->lines()->delete();
$cart->addresses()->delete();
$cart->delete();
});
});
});

$this->info('Prune complete');
}
}
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 @@ -204,7 +206,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
22 changes: 16 additions & 6 deletions packages/core/src/Managers/DiscountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,19 @@ 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()
)
)
->orWhere(fn ($query) => $query->productVariants(
$value->lines->pluck('purchasable.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(),
['condition', 'limitation']
)
);
});
}
)->when(
)
->when(
$cart?->coupon_code,
function ($query, $value) {
return $query->where(function ($query) use ($value) {
Expand Down Expand Up @@ -209,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
21 changes: 13 additions & 8 deletions packages/core/src/Models/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
use Lunar\Base\BaseModel;
use Lunar\Base\Traits\HasChannels;
use Lunar\Base\Traits\HasCustomerGroups;
Expand Down Expand Up @@ -167,20 +168,22 @@ public function scopeActive(Builder $query): Builder
/**
* Return the products scope.
*/
public function scopeProducts(Builder $query, iterable $productIds = [], string $type = null): Builder
public function scopeProducts(Builder $query, iterable $productIds = [], array|string $types = []): Builder
{
if (is_array($productIds)) {
$productIds = collect($productIds);
}

$types = Arr::wrap($types);

return $query->where(
fn ($subQuery) => $subQuery->whereDoesntHave('purchasables')
fn ($subQuery) => $subQuery->whereDoesntHave('purchasables', fn ($query) => $query->when($types, fn ($query) => $query->whereIn('type', $types)))
->orWhereHas('purchasables',
fn ($relation) => $relation->whereIn('purchasable_id', $productIds)
->wherePurchasableType(Product::class)
->when(
$type,
fn ($query) => $query->whereType($type)
$types,
fn ($query) => $query->whereIn('type', $types)
)
)
);
Expand All @@ -189,20 +192,22 @@ public function scopeProducts(Builder $query, iterable $productIds = [], string
/**
* Return the product variants scope.
*/
public function scopeProductVariants(Builder $query, iterable $variantIds = [], string $type = null): Builder
public function scopeProductVariants(Builder $query, iterable $variantIds = [], array|string $types = []): Builder
{
if (is_array($variantIds)) {
$variantIds = collect($variantIds);
}

$types = Arr::wrap($types);

return $query->where(
fn ($subQuery) => $subQuery->whereDoesntHave('purchasables')
fn ($subQuery) => $subQuery->whereDoesntHave('purchasables', fn ($query) => $query->when($types, fn ($query) => $query->whereIn('type', $types)))
->orWhereHas('purchasables',
fn ($relation) => $relation->whereIn('purchasable_id', $variantIds)
->wherePurchasableType(ProductVariant::class)
->when(
$type,
fn ($query) => $query->whereType($type)
$types,
fn ($query) => $query->whereIn('type', $types)
)
)
);
Expand Down
Loading

0 comments on commit 79b603d

Please sign in to comment.