diff --git a/CHANGELOG.md b/CHANGELOG.md index 99c89cf..43a4c99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index a84cf0d..200a7d0 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -25,6 +25,6 @@ "developer": "Ether", "developerUrl": "https://ethercreative.co.uk", "class": "ether\\cartnotices\\CartNotices", - "schemaVersion": "1.0.0" + "schemaVersion": "1.0.1" } } \ No newline at end of file diff --git a/src/controllers/NoticeController.php b/src/controllers/NoticeController.php index 6f3e583..466b331 100644 --- a/src/controllers/NoticeController.php +++ b/src/controllers/NoticeController.php @@ -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 = []; diff --git a/src/elements/Notice.php b/src/elements/Notice.php index e6d199f..6e27b2e 100644 --- a/src/elements/Notice.php +++ b/src/elements/Notice.php @@ -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; @@ -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'), ]; } @@ -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'; @@ -337,17 +347,19 @@ 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(); } @@ -355,15 +367,17 @@ public function afterSave (bool $isNew) { $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, diff --git a/src/elements/db/NoticeQuery.php b/src/elements/db/NoticeQuery.php index 25083ea..bfaa7f9 100644 --- a/src/elements/db/NoticeQuery.php +++ b/src/elements/db/NoticeQuery.php @@ -129,17 +129,30 @@ protected function beforePrepare (): bool // Minimum Amount // --------------------------------------------------------------------- - $sql = <<getDb()->getIsMysql()) + { + $sql = <<= COALESCE([[cart-notices.threshold]], 0) +SQL; + } + else + { + $sql = <<= 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(), ] ); diff --git a/src/migrations/Install.php b/src/migrations/Install.php index 6353c66..783e303 100644 --- a/src/migrations/Install.php +++ b/src/migrations/Install.php @@ -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(), diff --git a/src/migrations/m210107_092937_add_exclude_columns_to_notices_table.php b/src/migrations/m210107_092937_add_exclude_columns_to_notices_table.php new file mode 100644 index 0000000..9ca67d3 --- /dev/null +++ b/src/migrations/m210107_092937_add_exclude_columns_to_notices_table.php @@ -0,0 +1,38 @@ +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'); + } +} diff --git a/src/templates/_edit.twig b/src/templates/_edit.twig index 953f15e..178c743 100644 --- a/src/templates/_edit.twig +++ b/src/templates/_edit.twig @@ -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') }} +
+ {{ 'Tax will be excluded when checking the cart total'|t('cart-notices') }} +
+ {% 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') }} +
+ {{ 'Shipping will be excluded when checking the cart total'|t('cart-notices') }} +
+ {% 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'), diff --git a/src/translations/en/cart-notices.php b/src/translations/en/cart-notices.php index 21cb726..3ff3611 100644 --- a/src/translations/en/cart-notices.php +++ b/src/translations/en/cart-notices.php @@ -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',