Skip to content

Commit 5b3779b

Browse files
committed
Issue #2901322 by bojanz: Rename PercentageOffBase setting 'amount' to 'percentage'
1 parent 6380291 commit 5b3779b

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

modules/promotion/src/Plugin/Commerce/PromotionOffer/PercentageOffBase.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,31 @@ abstract class PercentageOffBase extends PromotionOfferBase {
1414
*/
1515
public function defaultConfiguration() {
1616
return [
17-
'amount' => '0',
17+
'percentage' => '0',
1818
] + parent::defaultConfiguration();
1919
}
2020

21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function setConfiguration(array $configuration) {
25+
parent::setConfiguration($configuration);
26+
27+
if (isset($this->configuration['amount'])) {
28+
// The 'amount' key was renamed to 'percentage' in 2.0-rc2.
29+
$this->configuration['percentage'] = $this->configuration['amount'];
30+
unset($this->configuration['amount']);
31+
}
32+
}
33+
2134
/**
2235
* Gets the percentage.
2336
*
2437
* @return string
2538
* The percentage.
2639
*/
2740
public function getPercentage() {
28-
return (string) $this->configuration['amount'];
41+
return (string) $this->configuration['percentage'];
2942
}
3043

3144
/**
@@ -34,16 +47,16 @@ public function getPercentage() {
3447
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
3548
$form += parent::buildConfigurationForm($form, $form_state);
3649

37-
$form['amount'] = [
50+
$form['percentage'] = [
3851
'#type' => 'commerce_number',
3952
'#title' => $this->t('Percentage'),
40-
'#default_value' => $this->configuration['amount'] * 100,
53+
'#default_value' => $this->configuration['percentage'] * 100,
4154
'#maxlength' => 255,
42-
'#required' => TRUE,
4355
'#min' => 0,
4456
'#max' => 100,
4557
'#size' => 4,
4658
'#field_suffix' => $this->t('%'),
59+
'#required' => TRUE,
4760
];
4861

4962
return $form;
@@ -54,8 +67,8 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
5467
*/
5568
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
5669
$values = $form_state->getValue($form['#parents']);
57-
if (empty($values['amount'])) {
58-
$form_state->setError($form, $this->t('Percentage amount cannot be empty.'));
70+
if (empty($values['percentage'])) {
71+
$form_state->setError($form, $this->t('Percentage must be a positive number.'));
5972
}
6073
}
6174

@@ -66,7 +79,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
6679
parent::submitConfigurationForm($form, $form_state);
6780

6881
$values = $form_state->getValue($form['#parents']);
69-
$this->configuration['amount'] = (string) ($values['amount'] / 100);
82+
$this->configuration['percentage'] = (string) ($values['percentage'] / 100);
7083
}
7184

7285
}

modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionElementTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function setUp() {
8282
'offer' => [
8383
'target_plugin_id' => 'order_percentage_off',
8484
'target_plugin_configuration' => [
85-
'amount' => '0.10',
85+
'percentage' => '0.10',
8686
],
8787
],
8888
'start_date' => '2017-01-01',

modules/promotion/tests/src/FunctionalJavascript/CouponRedemptionPaneTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function setUp() {
8686
'offer' => [
8787
'target_plugin_id' => 'order_percentage_off',
8888
'target_plugin_configuration' => [
89-
'amount' => '0.10',
89+
'percentage' => '0.10',
9090
],
9191
],
9292
'start_date' => '2017-01-01',

modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public function testCreatePromotion() {
5050
$this->getSession()->getPage()->fillField('name[0][value]', $name);
5151
$this->getSession()->getPage()->selectFieldOption('offer[0][target_plugin_id]', 'order_item_percentage_off');
5252
$this->waitForAjaxToFinish();
53-
$this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][order_item_percentage_off][amount]', '10.0');
53+
$this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][order_item_percentage_off][percentage]', '10.0');
5454

5555
// Change, assert any values reset.
5656
$this->getSession()->getPage()->selectFieldOption('offer[0][target_plugin_id]', 'order_percentage_off');
5757
$this->waitForAjaxToFinish();
58-
$this->assertSession()->fieldValueNotEquals('offer[0][target_plugin_configuration][order_percentage_off][amount]', '10.0');
59-
$this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][order_percentage_off][amount]', '10.0');
58+
$this->assertSession()->fieldValueNotEquals('offer[0][target_plugin_configuration][order_percentage_off][percentage]', '10.0');
59+
$this->getSession()->getPage()->fillField('offer[0][target_plugin_configuration][order_percentage_off][percentage]', '10.0');
6060

6161
// Confirm the integrity of the conditions UI.
6262
foreach (['order', 'product', 'customer'] as $condition_group) {
@@ -115,7 +115,7 @@ public function testCreatePromotionWithEndDate() {
115115
$name = $this->randomMachineName(8);
116116
$edit = [
117117
'name[0][value]' => $name,
118-
'offer[0][target_plugin_configuration][order_percentage_off][amount]' => '10.0',
118+
'offer[0][target_plugin_configuration][order_percentage_off][percentage]' => '10.0',
119119
];
120120

121121
// Set an end date.
@@ -170,7 +170,7 @@ public function testEditPromotion() {
170170
$new_promotion_name = $this->randomMachineName(8);
171171
$edit = [
172172
'name[0][value]' => $new_promotion_name,
173-
'offer[0][target_plugin_configuration][order_item_percentage_off][amount]' => '20',
173+
'offer[0][target_plugin_configuration][order_item_percentage_off][percentage]' => '20',
174174
];
175175
$this->submitForm($edit, 'Save');
176176

modules/promotion/tests/src/Kernel/PromotionOfferTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ public function testOrderPercentageOff() {
112112
'offer' => [
113113
'target_plugin_id' => 'order_percentage_off',
114114
'target_plugin_configuration' => [
115-
'amount' => '0.10',
115+
'percentage' => '0.10',
116116
],
117117
],
118118
]);
119119
$promotion->save();
120120

121121
/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */
122122
$offer_field = $promotion->get('offer')->first();
123-
$this->assertEquals('0.10', $offer_field->target_plugin_configuration['amount']);
123+
$this->assertEquals('0.10', $offer_field->target_plugin_configuration['percentage']);
124124

125125
$promotion->apply($this->order);
126126
$this->assertEquals(1, count($this->order->getAdjustments()));
@@ -218,15 +218,15 @@ public function testProductPercentageOff() {
218218
'offer' => [
219219
'target_plugin_id' => 'order_item_percentage_off',
220220
'target_plugin_configuration' => [
221-
'amount' => '0.50',
221+
'percentage' => '0.50',
222222
],
223223
],
224224
]);
225225
$promotion->save();
226226

227227
/** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $offer_field */
228228
$offer_field = $promotion->get('offer')->first();
229-
$this->assertEquals('0.50', $offer_field->target_plugin_configuration['amount']);
229+
$this->assertEquals('0.50', $offer_field->target_plugin_configuration['percentage']);
230230

231231
$this->container->get('commerce_order.order_refresh')->refresh($this->order);
232232
$this->order = $this->reloadEntity($this->order);

modules/tax/src/Plugin/Commerce/TaxType/Custom.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function setConfiguration(array $configuration) {
9191

9292
foreach ($this->configuration['rates'] as &$rate) {
9393
if (isset($rate['amount'])) {
94-
// The 'amount' property was renamed to 'percentage' in 2.0-rc2.
94+
// The 'amount' key was renamed to 'percentage' in 2.0-rc2.
9595
$rate['percentage'] = $rate['amount'];
9696
unset($rate['amount']);
9797
}

0 commit comments

Comments
 (0)