Skip to content

Commit

Permalink
Merge pull request #3077 from craftcms/bugfix/discount-input-number-n…
Browse files Browse the repository at this point in the history
…ormalization

Fixes #3067 number normalization on discount inputs
  • Loading branch information
nfourtythree authored Feb 1, 2023
2 parents f40f602 + 8948bd3 commit 3ee2a45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/controllers/DiscountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,28 @@ public function actionSave(): void
$discount->hasFreeShippingForOrder = (bool)$this->request->getBodyParam('hasFreeShippingForOrder');
$discount->excludeOnSale = (bool)$this->request->getBodyParam('excludeOnSale');
$discount->couponFormat = $this->request->getBodyParam('couponFormat', Coupons::DEFAULT_COUPON_FORMAT);
$discount->perUserLimit = $this->request->getBodyParam('perUserLimit');
$discount->perEmailLimit = $this->request->getBodyParam('perEmailLimit');
$discount->totalDiscountUseLimit = $this->request->getBodyParam('totalDiscountUseLimit');
$discount->perUserLimit = (int)$this->request->getBodyParam('perUserLimit');
$discount->perEmailLimit = (int)$this->request->getBodyParam('perEmailLimit');
$discount->totalDiscountUseLimit = (int)$this->request->getBodyParam('totalDiscountUseLimit');
$discount->ignoreSales = (bool)$this->request->getBodyParam('ignoreSales');
$discount->categoryRelationshipType = $this->request->getBodyParam('categoryRelationshipType');
$discount->baseDiscountType = $this->request->getBodyParam('baseDiscountType') ?: DiscountRecord::BASE_DISCOUNT_TYPE_VALUE;
$discount->appliedTo = $this->request->getBodyParam('appliedTo') ?: DiscountRecord::APPLIED_TO_MATCHING_LINE_ITEMS;
$discount->orderConditionFormula = $this->request->getBodyParam('orderConditionFormula');

$baseDiscount = $this->request->getBodyParam('baseDiscount') ?: 0;
$baseDiscount = preg_replace('/[^0-9\.\-\,]/', '', $baseDiscount);
$baseDiscount = Localization::normalizeNumber($baseDiscount);
$discount->baseDiscount = $baseDiscount * -1;

$perItemDiscount = $this->request->getBodyParam('perItemDiscount') ?: 0;
$perItemDiscount = preg_replace('/[^0-9\.\-\,]/', '', $perItemDiscount);
$perItemDiscount = Localization::normalizeNumber($perItemDiscount);
$discount->perItemDiscount = $perItemDiscount * -1;

$discount->purchaseTotal = Localization::normalizeNumber($this->request->getBodyParam('purchaseTotal', 0));
$purchaseTotal = $this->request->getBodyParam('purchaseTotal', 0);
$purchaseTotal = preg_replace('/[^0-9\.\-\,]/', '', $purchaseTotal);
$discount->purchaseTotal = (float)Localization::normalizeNumber($purchaseTotal);

$date = $this->request->getBodyParam('dateFrom');
if ($date) {
Expand All @@ -174,7 +178,9 @@ public function actionSave(): void
$discount->dateTo = $dateTime;
}

$discount->percentDiscount = -Localization::normalizePercentage($this->request->getBodyParam('percentDiscount'));
$percentDiscount = $this->request->getBodyParam('percentDiscount', 0);
$percentDiscount = preg_replace('/[^0-9\.\-\,]/', '', $percentDiscount);
$discount->percentDiscount = -Localization::normalizePercentage($percentDiscount);

// Set purchasable conditions
if ($discount->allPurchasables = (bool)$this->request->getBodyParam('allPurchasables')) {
Expand Down
13 changes: 9 additions & 4 deletions src/templates/promotions/discounts/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,10 @@
id: 'perUserLimit',
name: 'perUserLimit',
value: discount.perUserLimit,
type: 'number',
min: '0',
size: 10
step: '1',
size: 5
}) }}
</div>
<div>
Expand Down Expand Up @@ -424,8 +426,10 @@
id: 'perEmailLimit',
name: 'perEmailLimit',
value: discount.perEmailLimit,
type: 'number',
min: '0',
size: 10
step: '1',
size: 5
}) }}
</div>
<div>
Expand Down Expand Up @@ -454,8 +458,10 @@
id: 'totalDiscountUseLimit',
name: 'totalDiscountUseLimit',
value: discount.totalDiscountUseLimit,
type: 'number',
min: '0',
size: 10
step: '1',
size: 5
}) }}
</div>
<div>
Expand Down Expand Up @@ -570,7 +576,6 @@
id: 'baseDiscount',
name: 'baseDiscount',
type: 'text',
step: 'any',
size: 10,
value: discount.baseDiscount ? discount.baseDiscount|number : 0,
}) }}
Expand Down

0 comments on commit 3ee2a45

Please sign in to comment.