Skip to content

Commit

Permalink
Add exclude tax & shipping options to min amount notice
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Jan 7, 2021
1 parent 34657f0 commit 2b6560a
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 61 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.4 - 2021-01-07
### Added
- Add exclude tax & shipping options to min amount notice

## 1.0.3 - 2021-01-06
### Fixed
- Fix issues when cart is empty or not saved
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ether/cart-notices",
"description": "Automatically show notices based off cart details",
"version": "1.0.3",
"version": "1.0.4",
"type": "craft-plugin",
"license": "proprietary",
"minimum-stability": "dev",
Expand All @@ -25,6 +25,6 @@
"developer": "Ether",
"developerUrl": "https://ethercreative.co.uk",
"class": "ether\\cartnotices\\CartNotices",
"schemaVersion": "1.0.0"
"schemaVersion": "1.0.1"
}
}
24 changes: 13 additions & 11 deletions src/controllers/NoticeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,19 @@ public function actionSave ()
$request->getParam('fieldsLocation', 'fields')
);

$notice->enabled = $request->getParam('enabled');
$notice->type = $request->getParam('type');
$notice->target = $request->getParam('target');
$notice->threshold = $request->getParam('threshold');
$notice->hour = $request->getParam('hour');
$notice->days = $request->getParam('days');
$notice->referer = $request->getParam('referer');
$notice->minQty = $request->getParam('minQty');
$notice->maxQty = $request->getParam('maxQty');
$notice->productIds = $request->getParam('products', []);
$notice->categoryIds = $request->getParam('categories', []);
$notice->enabled = $request->getParam('enabled');
$notice->type = $request->getParam('type');
$notice->target = $request->getParam('target');
$notice->threshold = $request->getParam('threshold');
$notice->excludeTax = $request->getParam('excludeTax');
$notice->excludeShipping = $request->getParam('excludeShipping');
$notice->hour = $request->getParam('hour');
$notice->days = $request->getParam('days');
$notice->referer = $request->getParam('referer');
$notice->minQty = $request->getParam('minQty');
$notice->maxQty = $request->getParam('maxQty');
$notice->productIds = $request->getParam('products', []);
$notice->categoryIds = $request->getParam('categories', []);

if ($notice->enabled === '') $notice->enabled = false;
if (!is_array($notice->productIds)) $notice->productIds = [];
Expand Down
80 changes: 47 additions & 33 deletions src/elements/Notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class Notice extends Element
/** @var float - Minimum amount: total must be >= this */
public $threshold;

/** @var bool - Minimum amount: exclude tax from total */
public $excludeTax = false;

/** @var bool - Minimum amount: exclude shipping from total */
public $excludeShipping = false;

/** @var int - Deadline: deadline hour, can be 1 - 24 */
public $hour;

Expand Down Expand Up @@ -181,19 +187,21 @@ protected static function defineActions (string $source = null): array
protected static function defineTableAttributes (): array
{
return [
'title' => Craft::t('app', 'Title'),
'type' => CartNotices::t('Type'),
'target' => CartNotices::t('Target'),
'threshold' => CartNotices::t('Threshold'),
'hour' => CartNotices::t('Hour'),
'days' => CartNotices::t('Days'),
'referer' => CartNotices::t('Referer'),
'minQty' => CartNotices::t('Min Qty'),
'maxQty' => CartNotices::t('Max Qty'),
'products' => CartNotices::t('Products'),
'categories' => CartNotices::t('Categories'),
'dateCreated' => Craft::t('app', 'Date Created'),
'dateUpdated' => Craft::t('app', 'Date Updated'),
'title' => Craft::t('app', 'Title'),
'type' => CartNotices::t('Type'),
'target' => CartNotices::t('Target'),
'threshold' => CartNotices::t('Threshold'),
'excludeTax' => CartNotices::t('Exclude Tax'),
'excludeShipping' => CartNotices::t('Exclude Shipping'),
'hour' => CartNotices::t('Hour'),
'days' => CartNotices::t('Days'),
'referer' => CartNotices::t('Referer'),
'minQty' => CartNotices::t('Min Qty'),
'maxQty' => CartNotices::t('Max Qty'),
'products' => CartNotices::t('Products'),
'categories' => CartNotices::t('Categories'),
'dateCreated' => Craft::t('app', 'Date Created'),
'dateUpdated' => Craft::t('app', 'Date Updated'),
];
}

Expand All @@ -206,6 +214,8 @@ public static function defaultTableAttributes (string $source): array
case Types::MinimumAmount:
$attrs[] = 'target';
$attrs[] = 'threshold';
$attrs[] = 'excludeTax';
$attrs[] = 'excludeShipping';
break;
case Types::Deadline:
$attrs[] = 'hour';
Expand Down Expand Up @@ -337,33 +347,37 @@ public function afterSave (bool $isNew)
{
$db->createCommand()
->insert('{{%cart-notices}}', [
'id' => $this->id,
'siteId' => $this->siteId,
'enabled' => $this->enabled,
'type' => $this->type,
'target' => $this->target,
'threshold' => $this->threshold,
'hour' => $this->hour,
'days' => Json::encode($this->days),
'referer' => $this->referer,
'minQty' => $this->minQty,
'maxQty' => $this->maxQty,
'id' => $this->id,
'siteId' => $this->siteId,
'enabled' => $this->enabled,
'type' => $this->type,
'target' => $this->target,
'threshold' => $this->threshold,
'excludeTax' => $this->excludeTax,
'excludeShipping' => $this->excludeShipping,
'hour' => $this->hour,
'days' => Json::encode($this->days),
'referer' => $this->referer,
'minQty' => $this->minQty,
'maxQty' => $this->maxQty,
])
->execute();
}
else
{
$db->createCommand()
->update('{{%cart-notices}}', [
'enabled' => $this->enabled,
'type' => $this->type,
'target' => $this->target,
'threshold' => $this->threshold,
'hour' => $this->hour,
'days' => Json::encode($this->days),
'referer' => $this->referer,
'minQty' => $this->minQty,
'maxQty' => $this->maxQty,
'enabled' => $this->enabled,
'type' => $this->type,
'target' => $this->target,
'threshold' => $this->threshold,
'excludeTax' => $this->excludeTax,
'excludeShipping' => $this->excludeShipping,
'hour' => $this->hour,
'days' => Json::encode($this->days),
'referer' => $this->referer,
'minQty' => $this->minQty,
'maxQty' => $this->maxQty,
], [
'id' => $this->id,
'siteId' => $this->siteId,
Expand Down
23 changes: 18 additions & 5 deletions src/elements/db/NoticeQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,30 @@ protected function beforePrepare (): bool
// Minimum Amount
// ---------------------------------------------------------------------

$sql = <<<SQL
if (Craft::$app->getDb()->getIsMysql())
{
$sql = <<<SQL
[[cart-notices.type]] = :type1 AND
:total - (IF([[cart-notices.excludeTax]], :tax, 0) + IF([[cart-notices.excludeShipping]], :shipping, 0)) <= COALESCE([[cart-notices.target]], 99999999) AND
:total - (IF([[cart-notices.excludeTax]], :tax, 0) + IF([[cart-notices.excludeShipping]], :shipping, 0)) >= COALESCE([[cart-notices.threshold]], 0)
SQL;
}
else
{
$sql = <<<SQL
[[cart-notices.type]] = :type1 AND
:total <= COALESCE([[cart-notices.target]], 99999999) AND
:total >= COALESCE([[cart-notices.threshold]], 0)
:total - ((case when [[cart-notices.excludeTax]] then :tax else 0 end) + (case when [[cart-notices.excludeShipping]] then :shipping else 0 end)) <= COALESCE([[cart-notices.target]], 99999999) AND
:total - ((case when [[cart-notices.excludeTax]] then :tax else 0 end) + (case when [[cart-notices.excludeShipping]] then :shipping else 0 end)) >= COALESCE([[cart-notices.threshold]], 0)
SQL;
}

$this->subQuery->orWhere(
$sql,
[
'type1' => Types::MinimumAmount,
'total' => $cart->totalPrice
'type1' => Types::MinimumAmount,
'total' => $cart->getTotalPrice(),
'tax' => $cart->getTotalTax(),
'shipping' => $cart->getTotalShippingCost(),
]
);

Expand Down
22 changes: 12 additions & 10 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ public function safeUp ()
'id' => $this->primaryKey(),
'siteId' => $this->integer()->notNull(),

'enabled' => $this->boolean()->notNull(),
'type' => $this->string()->notNull(),
'target' => $this->float(),
'threshold' => $this->float(),
'hour' => $this->integer(),
'days' => $this->string(),
'referer' => $this->string(255),
'minQty' => $this->integer(),
'maxQty' => $this->integer(),
'sortOrder' => $this->smallInteger()->null(),
'enabled' => $this->boolean()->notNull(),
'type' => $this->string()->notNull(),
'target' => $this->float(),
'threshold' => $this->float(),
'hour' => $this->integer(),
'days' => $this->string(),
'referer' => $this->string(255),
'minQty' => $this->integer(),
'maxQty' => $this->integer(),
'sortOrder' => $this->smallInteger()->null(),
'excludeTax' => $this->boolean(),
'excludeShipping' => $this->boolean(),

'dateCreated' => $this->dateTime()->notNull(),
'dateUpdated' => $this->dateTime()->notNull(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace ether\cartnotices\migrations;

use Craft;
use craft\db\Migration;

/**
* m210107_092937_add_exclude_columns_to_notices_table migration.
*/
class m210107_092937_add_exclude_columns_to_notices_table extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$this->addColumn(
'{{%cart-notices}}',
'excludeTax',
$this->boolean()
);
$this->addColumn(
'{{%cart-notices}}',
'excludeShipping',
$this->boolean()
);
}

/**
* @inheritdoc
*/
public function safeDown()
{
$this->dropColumn('{{%cart-notices}}', 'excludeTax');
$this->dropColumn('{{%cart-notices}}', 'excludeShipping');
}
}
48 changes: 48 additions & 0 deletions src/templates/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,54 @@

{# Minimum Amount #}

{% embed '_includes/forms/field' with {
notice: notice,
errors: notice.getErrors('excludeTax'),
} only %}
{% import '_includes/forms' as forms %}
{% block attr %}
v-bind:style="show('minimum-amount')"
{% endblock %}
{% set label %}
{{ 'Exclude Tax'|t('cart-notices') }}
<div class="info">
{{ 'Tax will be excluded when checking the cart total'|t('cart-notices') }}
</div>
{% endset %}
{% set input %}
{{ forms.lightswitch({
name: 'excludeTax',
id: 'excludeTax',
on: notice.excludeTax,
value: 1,
}) }}
{% endset %}
{% endembed %}

{% embed '_includes/forms/field' with {
notice: notice,
errors: notice.getErrors('excludeShipping'),
} only %}
{% import '_includes/forms' as forms %}
{% block attr %}
v-bind:style="show('minimum-amount')"
{% endblock %}
{% set label %}
{{ 'Exclude Shipping'|t('cart-notices') }}
<div class="info">
{{ 'Shipping will be excluded when checking the cart total'|t('cart-notices') }}
</div>
{% endset %}
{% set input %}
{{ forms.lightswitch({
name: 'excludeShipping',
id: 'excludeShipping',
on: notice.excludeShipping,
value: 1,
}) }}
{% endset %}
{% endembed %}

{% embed '_includes/forms/field' with {
notice: notice,
errors: notice.getErrors('target'),
Expand Down
8 changes: 8 additions & 0 deletions src/translations/en/cart-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
'Cart total must be greater than or equal to this amount' =>
'Cart total must be greater than or equal to this amount',

'Exclude Tax' => 'Exclude Tax',
'Tax will be excluded when checking the cart total' =>
'Tax will be excluded when checking the cart total',

'Exclude Shipping' => 'Exclude Shipping',
'Shipping will be excluded when checking the cart total' =>
'Shipping will be excluded when checking the cart total',

'Hour' => 'Hour',
'Deadline hour, can be 1 - 24' => 'Deadline hour, can be 1 - 24',

Expand Down

0 comments on commit 2b6560a

Please sign in to comment.