Skip to content

Commit

Permalink
Actually add cart restore functionality (oops)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Jun 25, 2019
1 parent 7d64ed8 commit 3c1288d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.5 - 2019-06-25
### Added
- Actually add cart restore functionality (oops).

## 1.0.4 - 2019-06-24
### Added
- Added alert to the Mailchimp CP section telling the user that syncing is disabled (if it is).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ether/mailchimp-commerce",
"description": "Mailchimp integration with Craft Commerce",
"version": "1.0.4",
"version": "1.0.5",
"type": "craft-plugin",
"keywords": [
"mailchimp",
Expand Down
40 changes: 37 additions & 3 deletions src/controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
namespace ether\mc\controllers;

use Craft;
use craft\commerce\Plugin as Commerce;
use craft\errors\MissingComponentException;
use craft\web\Controller;
use ether\mc\MailchimpCommerce;
use yii\web\BadRequestHttpException;
use yii\web\Response;

/**
* Class OrderController
Expand All @@ -20,12 +25,41 @@
class OrderController extends Controller
{

protected $allowAnonymous = true;

/**
* Attempts to restore an abandoned cart
*
* @return Response
* @throws MissingComponentException
* @throws BadRequestHttpException
*/
public function actionRestore ()
{
$commerce = Commerce::getInstance();
$settings = MailchimpCommerce::getInstance()->getSettings();
$session = Craft::$app->getSession();

$number = Craft::$app->getRequest()->getRequiredBodyParam('number');
$order = $commerce->getOrders()->getOrderByNumber($number);

if (!$order)
{
$session->setError($settings->expiredCartError);
return $this->redirect($settings->abandonedCartRestoreUrl);
}

if ($order->isCompleted)
{
$session->setError($settings->completedCartError);
return $this->redirect($settings->abandonedCartRestoreUrl);
}

$commerce->getCarts()->forgetCart();
$session->set('commerce_cart', $number);

// TODO: Set order to be active cart, redirect to checkout
// (will need setting for checkout url)
$session->setNotice($settings->cartRestoredNotice);
return $this->redirect($settings->abandonedCartRestoreUrl);
}

}
}
16 changes: 16 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ class Settings extends Model
*/
public $abandonedCartRestoreUrl;

/**
* @var string The error notice sent when an abandoned cart has expired.
*/
public $expiredCartError = 'Your cart has expired!';

/**
* @var string The error notice sent when an abandoned cart that has
* already been completed is attempted to be restored.
*/
public $completedCartError = 'You\'ve already completed this order!';

/**
* @var string The success notice sent when an abandoned cart is restored.
*/
public $cartRestoredNotice = 'Your cart has been restored!';

// Products
// -------------------------------------------------------------------------

Expand Down
21 changes: 21 additions & 0 deletions src/templates/_settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@
placeholder: 'i.e. /cart',
}) }}

{{ forms.textField({
name: 'expiredCartError',
label: 'Expired Cart Error'|t('mailchimp-commerce'),
instructions: 'The error notice sent when an abandoned cart has expired.'|t('mailchimp-commerce'),
value: settings.expiredCartError,
}) }}

{{ forms.textField({
name: 'completedCartError',
label: 'Completed Cart Error'|t('mailchimp-commerce'),
instructions: 'The error notice sent when an abandoned cart that has already been completed is attempted to be restored.'|t('mailchimp-commerce'),
value: settings.completedCartError,
}) }}

{{ forms.textField({
name: 'cartRestoredNotice',
label: 'Cart Restored Notice'|t('mailchimp-commerce'),
instructions: 'The success notice sent when an abandoned cart is restored.'|t('mailchimp-commerce'),
value: settings.cartRestoredNotice,
}) }}

{% endnamespace %}

{% endblock %}
11 changes: 11 additions & 0 deletions src/translations/en/mailchimp-commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@
'The URL that will be redirected to after an abandoned cart is restored.' =>
'The URL that will be redirected to after an abandoned cart is restored.',

'Expired Cart Error' => 'Expired Cart Error',
'The error notice sent when an abandoned cart has expired.' => 'The error notice sent when an abandoned cart has expired.',

'Completed Cart Error' => 'Completed Cart Error',
'The error notice sent when an abandoned cart that has already been completed is attempted to be restored.' =>
'The error notice sent when an abandoned cart that has already been completed is attempted to be restored.',

'Cart Restored Notice' => 'Cart Restored Notice',
'The success notice sent when an abandoned cart is restored.' => 'The success notice sent when an abandoned cart is restored.',


// Jobs
// =========================================================================

Expand Down

0 comments on commit 3c1288d

Please sign in to comment.