Skip to content

Commit

Permalink
Fixed #592
Browse files Browse the repository at this point in the history
Fixed #592 saving discount percentage with comma decimal separator
  • Loading branch information
lukeholder committed Aug 22, 2019
2 parents 21cb283 + 9c98da0 commit ac58e0c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
12 changes: 6 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

### Changed
- Calling `order.pdfUrl` no longer pre-renders the order PDF before returning the download URL, improving performance. ([#962](https://github.com/craftcms/commerce/issues/962))
- Updated Product HUD to align with other elements. ([#418](https://github.com/craftcms/commerce/issues/418))
- Updated Qty and Stock validation to show multiple LineItem errors and out of stock items. ([#546](https://github.com/craftcms/commerce/issues/546))
- Updated error message on Product validation if there are errors with the variants. ([#953](https://github.com/craftcms/commerce/issues/953))
- Updated Commerce emails to create Message class from mailer config to be consistent with core.

### Fixed
- Fixed a bug where the orders revenue chart wasn't showing the correct currency. ([#792](https://github.com/craftcms/commerce/issues/792))
- Fixed a bug where the revenue widget chart wasn't showing the correct currency. ([#792](https://github.com/craftcms/commerce/issues/792))
- Fixed some British message translations.
- Fixed a JavaScript error when using the Products HUD outside of Commerce.
- Fixed a bug where decimals were being stripped in locales that use commas as separtors ([#592](https://github.com/craftcms/commerce/issues/592))
- Fixed a javascript error when displaying the Product editor HUD. ([#418](https://github.com/craftcms/commerce/issues/418))
- Fixed a bug where sites with a large number of variants might not update properly when updating. ([#964](https://github.com/craftcms/commerce/issues/964))
- Fixed a bug where Commerce emails were not sending with a From Name ([#939](https://github.com/craftcms/commerce/issues/939))
- Fixed a PHP error when saving a Product with the editor HUD . ([#958](https://github.com/craftcms/commerce/issues/958))
- Fixed a bug where the “Purchase Total” discount condition would only save whole numbers. ([#966]()https://github.com/craftcms/commerce/pull/966)
- Fixed a bug where products showed a blank validation error message when their variants had errors. ([#546](https://github.com/craftcms/commerce/issues/546))

## 2.1.12 - 2019-08-15

Expand Down Expand Up @@ -327,7 +327,7 @@
- Added the Store Location setting.
- Customers can now save their credit cards or payment sources stored as tokens in Commerce so customers don’t need to enter their card number on subsequent checkouts. ([#21](https://github.com/craftcms/commerce/issues/21))
- Any custom purchasable can now have sales and discounts applied to them.
- Sales and discouts can now be set on categories of products or purchasables.
- Sales and discounts can now be set on categories of products or purchasables.
- Customers can now set their primary default shipping and billing addresses in their address book.
- It’s now possible to export orders as CSV, ODS, XSL, and XLSX, from the Orders index page. ([#222](https://github.com/craftcms/commerce/issues/222))
- Orders can now have custom-formatted, sequential reference numbers. ([#184](https://github.com/craftcms/commerce/issues/184))
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function editions(): array
/**
* @inheritDoc
*/
public $schemaVersion = '2.1.06';
public $schemaVersion = '2.1.07';

/**
* @inheritdoc
Expand Down
30 changes: 23 additions & 7 deletions src/controllers/DiscountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
use craft\helpers\ArrayHelper;
use craft\helpers\DateTimeHelper;
use craft\helpers\Json;
use craft\helpers\Localization;
use craft\i18n\Locale;
use function explode;
use function get_class;
use yii\web\HttpException;
use yii\web\Response;
use function explode;
use function get_class;

/**
* Class Discounts Controller
Expand Down Expand Up @@ -94,11 +95,8 @@ public function actionSave()
$discount->description = $request->getBodyParam('description');
$discount->enabled = (bool)$request->getBodyParam('enabled');
$discount->stopProcessing = (bool)$request->getBodyParam('stopProcessing');
$discount->purchaseTotal = $request->getBodyParam('purchaseTotal');
$discount->purchaseQty = $request->getBodyParam('purchaseQty');
$discount->maxPurchaseQty = $request->getBodyParam('maxPurchaseQty');
$discount->baseDiscount = $request->getBodyParam('baseDiscount');
$discount->perItemDiscount = $request->getBodyParam('perItemDiscount');
$discount->percentDiscount = $request->getBodyParam('percentDiscount');
$discount->percentageOffSubject = $request->getBodyParam('percentageOffSubject');
$discount->hasFreeShippingForMatchingItems = (bool)$request->getBodyParam('hasFreeShippingForMatchingItems');
Expand All @@ -109,8 +107,13 @@ public function actionSave()
$discount->perEmailLimit = $request->getBodyParam('perEmailLimit');
$discount->totalUseLimit = $request->getBodyParam('totalUseLimit');

$discount->baseDiscount = (float)$request->getBodyParam('baseDiscount') * -1;
$discount->perItemDiscount = (float)$request->getBodyParam('perItemDiscount') * -1;
$baseDiscount = Localization::normalizeNumber($request->getBodyParam('baseDiscount'));
$discount->baseDiscount = $baseDiscount * -1;

$perItemDiscount = Localization::normalizeNumber($request->getBodyParam('perItemDiscount'));
$discount->perItemDiscount = $perItemDiscount * -1;

$discount->purchaseTotal = Localization::normalizeNumber($request->getBodyParam('purchaseTotal'));

$date = $request->getBodyParam('dateFrom');
if ($date) {
Expand All @@ -128,6 +131,7 @@ public function actionSave()
$percentDiscountAmount = $request->getBodyParam('percentDiscount');
$localeData = Craft::$app->getLocale();
$percentSign = $localeData->getNumberSymbol(Locale::SYMBOL_PERCENT);
$percentDiscountAmount = Localization::normalizeNumber($percentDiscountAmount);
if (strpos($percentDiscountAmount, $percentSign) || (float)$percentDiscountAmount >= 1) {
$discount->percentDiscount = (float)$percentDiscountAmount / -100;
} else {
Expand Down Expand Up @@ -242,6 +246,18 @@ private function _populateVariables(&$variables)
$variables['groups'] = [];
}

if ($variables['discount']->baseDiscount != 0) {
$variables['discount']->baseDiscount = Craft::$app->formatter->asDecimal((float)$variables['discount']->baseDiscount * -1);
}

if ($variables['discount']->perItemDiscount != 0) {
$variables['discount']->perItemDiscount = Craft::$app->formatter->asDecimal((float)$variables['discount']->perItemDiscount * -1);
}

if ($variables['discount']->purchaseTotal != 0) {
$variables['discount']->purchaseTotal = Craft::$app->formatter->asDecimal((float)$variables['discount']->purchaseTotal);
}

$variables['categoryElementType'] = Category::class;
$variables['categories'] = null;
$categories = $categoryIds = [];
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function createTables()
'totalUses' => $this->integer()->notNull()->defaultValue(0)->unsigned(),
'dateFrom' => $this->dateTime(),
'dateTo' => $this->dateTime(),
'purchaseTotal' => $this->integer()->notNull()->defaultValue(0),
'purchaseTotal' => $this->decimal(14, 4)->notNull()->defaultValue(0),
'purchaseQty' => $this->integer()->notNull()->defaultValue(0),
'maxPurchaseQty' => $this->integer()->notNull()->defaultValue(0),
'baseDiscount' => $this->decimal(14, 4)->notNull()->defaultValue(0),
Expand Down
28 changes: 28 additions & 0 deletions src/migrations/m190821_150226_discount_purchaseTotal_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace craft\commerce\migrations;

use craft\db\Migration;

/**
* m190821_150226_discount_purchaseTotal_update migration.
*/
class m190821_150226_discount_purchaseTotal_update extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$this->alterColumn('{{%commerce_discounts}}', 'purchaseTotal', $this->decimal(14, 4));
}

/**
* @inheritdoc
*/
public function safeDown()
{
echo "m190821_150226_discount_purchaseTotal_update cannot be reverted.\n";
return false;
}
}
9 changes: 5 additions & 4 deletions src/templates/promotions/discounts/_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,14 @@
errors: discount.getErrors('categories'),
}) }}

{{ forms.textField({
{{ commerceForms.numberField({
label: "Purchase Total"|t('commerce'),
instructions: "Restrict the discount to only those orders where the customer has purchased a minimum total value of matching items."|t('commerce'),
id: 'purchaseTotal',
name: 'purchaseTotal',
value: discount.purchaseTotal,
value: discount.purchaseTotal != 0 ? discount.purchaseTotal : 0,
type: 'number',
step: 'any',
errors: discount.getErrors('purchaseTotal'),
}) }}

Expand Down Expand Up @@ -245,7 +246,7 @@
instructions: "The flat value which should discount the order’s total price. i.e “10” for $10 off."|t('commerce'),
id: 'baseDiscount',
name: 'baseDiscount',
value: discount.baseDiscount != 0 ? discount.baseDiscount * -1 : 0,
value: discount.baseDiscount != 0 ? discount.baseDiscount : 0,
type: 'number',
step: 'any',
errors: discount.getErrors('baseDiscount'),
Expand All @@ -256,7 +257,7 @@
instructions: "The flat value which should discount each item in the order. i.e “1” for $1 off."|t('commerce'),
id: 'perItemDiscount',
name: 'perItemDiscount',
value: discount.perItemDiscount != 0 ? discount.perItemDiscount * -1 : 0,
value: discount.perItemDiscount != 0 ? discount.perItemDiscount : 0,
type: 'number',
step: 'any',
errors: discount.getErrors('perItemDiscount'),
Expand Down

0 comments on commit ac58e0c

Please sign in to comment.