-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathClearCartDataCommand.php
57 lines (44 loc) · 1.49 KB
/
ClearCartDataCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Freshbitsweb\LaravelCartManager\Console\Commands;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
class ClearCartDataCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lcm_carts:clear_old';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Removes the cart data that was updated before the number of hours specified in cart_data_validity config.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (! Schema::hasTable('carts')) {
$this->error('No carts table found.');
exit;
}
$this->comment('Cleaning carts data...');
$validHours = config('cart_manager.cart_data_validity');
$query = resolve(config('cart_manager.cart_model'))::where('updated_at', '<', Carbon::now()->subHours($validHours));
$cartIds = $query->get(['id']);
if ($cartIds->isNotEmpty()) {
$cartsDeleted = $query->delete();
resolve(config('cart_manager.cart_item_model'))::whereIn('cart_id', $cartIds->pluck('id')->toArray())->delete();
$this->info("$cartsDeleted older cart record(s) removed from the table.");
} else {
$this->info('No older cart records found.');
}
$this->comment('Task complete.');
}
}