Skip to content

Commit

Permalink
Fixed #2824 move output formatting to display layer
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Jun 7, 2022
1 parent 901802b commit 009b630
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- Deprecated `craft\commerce\services\ProductType::pruneDeletedField()`.
- Deprecated `craft\commerce\services\Subscriptions::pruneDeletedField()`.

### Fixed
- Fixed a PHP error that could occur when saving a shipping rule. ([#2824](https://github.com/craftcms/commerce/issues/2824))

## 4.0.2 - 2022-06-03

### Fixed
Expand Down
33 changes: 0 additions & 33 deletions src/controllers/ShippingRulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,39 +108,6 @@ public function actionEdit(int $methodId = null, int $ruleId = null, ShippingRul
$variables['categoryShippingOptions'][] = ['label' => Craft::t('commerce', 'Disallow'), 'value' => ShippingRuleCategoryRecord::CONDITION_DISALLOW];
$variables['categoryShippingOptions'][] = ['label' => Craft::t('commerce', 'Require'), 'value' => ShippingRuleCategoryRecord::CONDITION_REQUIRE];

if ($variables['shippingRule'] instanceof ShippingRule) {
$categoryModels = $variables['shippingRule']->getShippingRuleCategories();
// Localize numbers
$localizeAttributes = [
'minTotal',
'maxTotal',
'minWeight',
'maxWeight',
'baseRate',
'perItemRate',
'weightRate',
'percentageRate',
'minRate',
'maxRate',
];

foreach ($localizeAttributes as $attr) {
if (isset($variables['shippingRule']->{$attr}) && $variables['shippingRule']->{$attr} !== null) {
$variables['shippingRule']->{$attr} = Craft::$app->getFormatter()->asDecimal((float)$variables['shippingRule']->{$attr});
}

if (!empty($categoryModels)) {
foreach ($categoryModels as $categoryModel) {
if (isset($categoryModel->{$attr}) && $categoryModel->{$attr} !== null) {
$categoryModel->{$attr} = Craft::$app->getFormatter()->asDecimal((float)$categoryModel->{$attr});
}
}
}
}

$variables['shippingRule']->setShippingRuleCategories($categoryModels);
}

return $this->renderTemplate('commerce/shipping/shippingrules/_edit', $variables);
}

Expand Down
32 changes: 16 additions & 16 deletions src/templates/shipping/shippingrules/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
{{ commerceForms.numberField({
name: 'minTotal',
label: 'Minimum'|t('commerce'),
value: shippingRule.minTotal,
value: craft.app.formatter.asDecimal(shippingRule.minTotal),
type: 'number',
min: 0,
step: 'any',
Expand All @@ -322,7 +322,7 @@
{{ commerceForms.numberField({
name: 'maxTotal',
label: 'Maximum'|t('commerce'),
value: shippingRule.maxTotal,
value: craft.app.formatter.asDecimal(shippingRule.maxTotal),
type: 'number',
min: 0,
step: 'any',
Expand Down Expand Up @@ -362,7 +362,7 @@
{{ commerceForms.numberField({
name: 'minWeight',
label: 'Minimum'|t('commerce'),
value: shippingRule.minWeight,
value: craft.app.formatter.asDecimal(shippingRule.minWeight),
type: 'number',
min: 0,
step: 'any',
Expand All @@ -374,7 +374,7 @@
{{ commerceForms.numberField({
name: 'maxWeight',
label: 'Maximum'|t('commerce'),
value: shippingRule.maxWeight,
value: craft.app.formatter.asDecimal(shippingRule.maxWeight),
type: 'number',
min: 0,
step: 'any',
Expand Down Expand Up @@ -430,7 +430,7 @@
label: "Base Rate"|t('commerce'),
name: 'baseRate',
instructions: "Shipping costs added to the order as a whole before percentage, item, and weight rates are applied. Set to zero to disable this rate. The whole rule, including this base rate, will not match and apply if the cart only contains non-shippable items like digital products."|t('commerce'),
value: shippingRule.baseRate,
value: craft.app.formatter.asDecimal(shippingRule.baseRate),
type: 'number',
min: 0,
step: 'any',
Expand All @@ -442,7 +442,7 @@
label: "Minimum Total Shipping Cost"|t('commerce'),
name: 'minRate',
instructions: "The minimum the customer should spend on shipping. Set to zero to disable."|t('commerce'),
value: shippingRule.minRate,
value: craft.app.formatter.asDecimal(shippingRule.minRate),
type: 'number',
min: 0,
step: 'any',
Expand All @@ -454,7 +454,7 @@
label: "Maximum Total Shipping Cost"|t('commerce'),
instructions: "The maximum the customer should spend on shipping. Set to zero to disable."|t('commerce'),
name: 'maxRate',
value: shippingRule.maxRate,
value: craft.app.formatter.asDecimal(shippingRule.maxRate),
type: 'number',
min: 0,
step: 'any',
Expand Down Expand Up @@ -483,7 +483,7 @@
<td class="topalign" data-title="{{ 'Per Item Rate'|t('commerce') }}">
{{ commerceForms.numberField({
name: 'perItemRate',
value: shippingRule.perItemRate,
value: craft.app.formatter.asDecimal(shippingRule.perItemRate),
type: 'number',
min: 0,
step: 'any',
Expand All @@ -494,7 +494,7 @@
<td class="topalign" data-title="{{ 'Weight Rate'|t('commerce') }}">
{{ commerceForms.numberField({
name: 'weightRate',
value: shippingRule.weightRate,
value: craft.app.formatter.asDecimal(shippingRule.weightRate),
type: 'number',
min: 0,
step: 'any',
Expand All @@ -505,7 +505,7 @@
<td class="topalign" data-title="{{ 'Percentage Rate'|t('commerce') }}">
{{ commerceForms.numberField({
name: 'percentageRate',
value: shippingRule.percentageRate,
value: craft.app.formatter.asDecimal(shippingRule.percentageRate),
type: 'number',
min: 0,
step: 'any',
Expand All @@ -522,9 +522,9 @@
</tr>
{% for shippingCategory in shippingCategories %}
{% set shippingCategoryRule = shippingRule.getShippingRuleCategories[shippingCategory.id] ?? null %}
{% set perItemRate = shippingCategoryRule ? shippingCategoryRule.perItemRate : '' %}
{% set weightRate = shippingCategoryRule ? shippingCategoryRule.weightRate : '' %}
{% set percentageRate = shippingCategoryRule ? shippingCategoryRule.percentageRate : '' %}
{% set perItemRate = shippingCategoryRule and not shippingCategoryRule.perItemRate is same as(null) ? craft.app.formatter.asDecimal(shippingCategoryRule.perItemRate) : '' %}
{% set weightRate = shippingCategoryRule and not shippingCategoryRule.weightRate is same as(null) ? craft.app.formatter.asDecimal(shippingCategoryRule.weightRate) : '' %}
{% set percentageRate = shippingCategoryRule and not shippingCategoryRule.percentageRate is same as(null) ? craft.app.formatter.asDecimal(shippingCategoryRule.percentageRate) : '' %}
{% set categoryCondition = shippingCategoryRule ? shippingCategoryRule.condition : '' %}

{% set id = shippingCategory.id %}
Expand All @@ -536,7 +536,7 @@
<td class="topalign" data-title="{{ 'Per Item Rate'|t('commerce') }}">
{{ commerceForms.numberField({
name: 'ruleCategories['~shippingCategory.id~'][perItemRate]',
placeholder: shippingRule.perItemRate,
placeholder: craft.app.formatter.asDecimal(shippingRule.perItemRate),
class: 'categoryPerItemRate',
value: perItemRate,
type: 'number',
Expand All @@ -548,7 +548,7 @@
{{ commerceForms.numberField({
name: 'ruleCategories['~shippingCategory.id~'][weightRate]',
class: 'categoryWeightRate',
placeholder: shippingRule.weightRate,
placeholder: craft.app.formatter.asDecimal(shippingRule.weightRate),
value: weightRate,
type: 'number',
step: 'any',
Expand All @@ -559,7 +559,7 @@
{{ commerceForms.numberField({
name: 'ruleCategories['~shippingCategory.id~'][percentageRate]',
class: 'categoryPercentageRate',
placeholder: shippingRule.percentageRate,
placeholder: craft.app.formatter.asDecimal(shippingRule.percentageRate),
value: percentageRate,
type: 'number',
step: 'any',
Expand Down

0 comments on commit 009b630

Please sign in to comment.