diff --git a/CHANGELOG.md b/CHANGELOG.md index 99b78bd1f2..9e4156f74d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/controllers/ShippingRulesController.php b/src/controllers/ShippingRulesController.php index 744b6e8079..b345a7cca7 100644 --- a/src/controllers/ShippingRulesController.php +++ b/src/controllers/ShippingRulesController.php @@ -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); } diff --git a/src/templates/shipping/shippingrules/_edit.twig b/src/templates/shipping/shippingrules/_edit.twig index 6c0424860e..304db48160 100644 --- a/src/templates/shipping/shippingrules/_edit.twig +++ b/src/templates/shipping/shippingrules/_edit.twig @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -483,7 +483,7 @@ {{ commerceForms.numberField({ name: 'perItemRate', - value: shippingRule.perItemRate, + value: craft.app.formatter.asDecimal(shippingRule.perItemRate), type: 'number', min: 0, step: 'any', @@ -494,7 +494,7 @@ {{ commerceForms.numberField({ name: 'weightRate', - value: shippingRule.weightRate, + value: craft.app.formatter.asDecimal(shippingRule.weightRate), type: 'number', min: 0, step: 'any', @@ -505,7 +505,7 @@ {{ commerceForms.numberField({ name: 'percentageRate', - value: shippingRule.percentageRate, + value: craft.app.formatter.asDecimal(shippingRule.percentageRate), type: 'number', min: 0, step: 'any', @@ -522,9 +522,9 @@ {% 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 %} @@ -536,7 +536,7 @@ {{ commerceForms.numberField({ name: 'ruleCategories['~shippingCategory.id~'][perItemRate]', - placeholder: shippingRule.perItemRate, + placeholder: craft.app.formatter.asDecimal(shippingRule.perItemRate), class: 'categoryPerItemRate', value: perItemRate, type: 'number', @@ -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', @@ -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',