Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Add artisan command to process abandoned carts
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbedard committed Apr 30, 2017
1 parent 42f2b6e commit 84fad2a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function boot()
*/
public function register()
{
$this->registerConsoleCommand('shop.abandon', 'Bedard\Shop\Console\Abandon');
}

/**
Expand Down Expand Up @@ -137,6 +138,17 @@ public function registerPermissions()
];
}

/**
* Register scheduled tasks.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function registerSchedule($schedule)
{
$schedule->command('shop:abandon')->everyTenMinutes();
}

/**
* Register settings models.
*
Expand Down
44 changes: 44 additions & 0 deletions console/Abandon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php namespace Bedard\Shop\Console;

use Bedard\Shop\Models\Cart;
use Illuminate\Console\Command;
use Lang;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class Abandon extends Command
{
/**
* @var string The console command name.
*/
protected $name = 'shop:abandon';

/**
* @var string The console command description.
*/
protected $description;

/**
* Constructor
*
* @return void
*/
public function __construct()
{
$this->description = Lang::get('bedard.shop::lang.console.abandon.description');

parent::__construct();
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
Cart::processAbandoned();

$this->output->writeln('<info>' . Lang::get('bedard.shop::lang.console.abandon.success') . '</info>');
}
}
10 changes: 10 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
'plural' => 'Categories',
],

//
// console
//
'console' => [
'abandon' => [
'description' => 'Process carts that have been abandoned',
'success' => 'Processed abandoned carts',
],
],

//
// drivers
//
Expand Down
12 changes: 12 additions & 0 deletions models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ protected function incrementUpdateCount()
$this->update_count++;
}

/**
* Process carts that have been abandoned.
*
* @return void
*/
public static function processAbandoned()
{
self::isAbandoned()->get()->each(function ($cart) {
$cart->abandon();
});
}

/**
* Reduce the available inventory.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/models/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,22 @@ public function test_setting_a_status_with_a_driver()

$this->assertEquals(get_class($driver), $data['statuses'][1]['pivot']['driver']);
}

public function test_processing_abandoned_carts()
{
// set the cart lifespan to 10 minutes
Settings::set('cart_lifespan', 10);

// create a cart was last updated 20 minutes ago
$cart = Factory::create(new Cart);
$cart->updated_at = Carbon::now()->subMinutes(20);
$cart->save();

// process the abandoned carts
Cart::processAbandoned();

// our cart should now be abandoned
$cart = Cart::find($cart->id);
$this->assertEquals(Carbon::now(), $cart->abandoned_at);
}
}

0 comments on commit 84fad2a

Please sign in to comment.