Skip to content

Commit 289a07e

Browse files
mitrpakabojanz
authored andcommitted
Issue #2894792 by mitrpaka, bojanz: Add Adjustment::getPercentage()
1 parent a2fd62f commit 289a07e

File tree

12 files changed

+83
-23
lines changed

12 files changed

+83
-23
lines changed

modules/order/src/Adjustment.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ final class Adjustment {
3030
*/
3131
protected $amount;
3232

33+
/**
34+
* The adjustment percentage.
35+
*
36+
* @var string
37+
*/
38+
protected $percentage;
39+
3340
/**
3441
* The source identifier of the adjustment.
3542
*
@@ -67,13 +74,20 @@ public function __construct(array $definition) {
6774
if (empty($types[$definition['type']])) {
6875
throw new \InvalidArgumentException(sprintf('%s is an invalid adjustment type.', $definition['type']));
6976
}
77+
if (!empty($definition['percentage'])) {
78+
if (is_float($definition['percentage'])) {
79+
throw new \InvalidArgumentException(sprintf('The provided percentage "%s" must be a string, not a float.', $definition['percentage']));
80+
}
81+
if (!is_numeric($definition['percentage'])) {
82+
throw new \InvalidArgumentException(sprintf('The provided percentage "%s" is not a numeric value.', $definition['percentage']));
83+
}
84+
}
7085

7186
$this->type = $definition['type'];
7287
$this->label = $definition['label'];
7388
$this->amount = $definition['amount'];
74-
if (!empty($definition['source_id'])) {
75-
$this->sourceId = $definition['source_id'];
76-
}
89+
$this->percentage = !empty($definition['percentage']) ? $definition['percentage'] : NULL;
90+
$this->sourceId = !empty($definition['source_id']) ? $definition['source_id'] : NULL;
7791
$this->included = !empty($definition['included']);
7892
}
7993

@@ -97,16 +111,6 @@ public function getLabel() {
97111
return $this->label;
98112
}
99113

100-
/**
101-
* Get the source identifier.
102-
*
103-
* @return string
104-
* The source identifier.
105-
*/
106-
public function getSourceId() {
107-
return $this->sourceId;
108-
}
109-
110114
/**
111115
* Gets the adjustment amount.
112116
*
@@ -121,7 +125,7 @@ public function getAmount() {
121125
* Gets whether the adjustment is positive.
122126
*
123127
* @return bool
124-
* TRUE if the adjustmnet is positive, FALSE otherwise.
128+
* TRUE if the adjustment is positive, FALSE otherwise.
125129
*/
126130
public function isPositive() {
127131
return $this->amount->getNumber() >= 0;
@@ -137,6 +141,27 @@ public function isNegative() {
137141
return $this->amount->getNumber() < 0;
138142
}
139143

144+
/**
145+
* Gets the adjustment percentage.
146+
*
147+
* @return string|null
148+
* The percentage as a decimal. For example, "0.2" for a 20% adjustment.
149+
* Otherwise NULL, if the adjustment was not calculated from a percentage.
150+
*/
151+
public function getPercentage() {
152+
return $this->percentage;
153+
}
154+
155+
/**
156+
* Get the source identifier.
157+
*
158+
* @return string
159+
* The source identifier.
160+
*/
161+
public function getSourceId() {
162+
return $this->sourceId;
163+
}
164+
140165
/**
141166
* Gets whether the adjustment is included in the base price.
142167
*

modules/order/src/Entity/Order.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,9 @@ public function collectAdjustments() {
324324
$multiplied_adjustment = new Adjustment([
325325
'type' => $adjustment->getType(),
326326
'label' => $adjustment->getLabel(),
327-
'source_id' => $adjustment->getSourceId(),
328327
'amount' => $adjustment->getAmount()->multiply($order_item->getQuantity()),
328+
'percentage' => $adjustment->getPercentage(),
329+
'source_id' => $adjustment->getSourceId(),
329330
'included' => $adjustment->isIncluded(),
330331
]);
331332
$adjustments[] = $multiplied_adjustment;

modules/order/tests/src/Functional/OrderAdminTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public function testEditOrder() {
125125
'type' => 'custom',
126126
'label' => '10% off',
127127
'amount' => new Price('-1.00', 'USD'),
128+
'percentage' => '0.1',
128129
]);
129130
$adjustments[] = new Adjustment([
130131
'type' => 'custom',

modules/order/tests/src/Kernel/AdjustmentItemTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function testAdjustmentItem() {
6868
'type' => 'custom',
6969
'label' => '10% off',
7070
'amount' => new Price('-1.00', 'USD'),
71+
'percentage' => '0.1',
7172
'source_id' => '1',
7273
]));
7374

modules/order/tests/src/Kernel/AdjustmentTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ public function invalidDefinitionProvider() {
6565
],
6666
'foo is an invalid adjustment type.',
6767
],
68+
[
69+
[
70+
'type' => 'custom',
71+
'label' => 'Foo',
72+
'amount' => new Price('1.00', 'USD'),
73+
'percentage' => 0.1,
74+
],
75+
'The provided percentage "0.1" must be a string, not a float.',
76+
],
77+
[
78+
[
79+
'type' => 'custom',
80+
'label' => 'Foo',
81+
'amount' => new Price('1.00', 'USD'),
82+
'percentage' => 'INVALID',
83+
],
84+
'The provided percentage "INVALID" is not a numeric value.',
85+
],
6886
];
6987
}
7088

@@ -93,14 +111,16 @@ public function testValidAdjustmentConstruct() {
93111
* @covers ::getAmount
94112
* @covers ::isPositive
95113
* @covers ::isNegative
96-
* @covers ::isIncluded
114+
* @covers ::getPercentage
97115
* @covers ::getSourceId
116+
* @covers ::isIncluded
98117
*/
99118
public function testAdjustmentMethods() {
100119
$definition = [
101120
'type' => 'custom',
102121
'label' => '10% off',
103122
'amount' => new Price('-1.00', 'USD'),
123+
'percentage' => '0.1',
104124
'source_id' => '1',
105125
'included' => TRUE,
106126
];
@@ -112,8 +132,9 @@ public function testAdjustmentMethods() {
112132
$this->assertEquals('USD', $adjustment->getAmount()->getCurrencyCode());
113133
$this->assertFalse($adjustment->isPositive());
114134
$this->assertTrue($adjustment->isNegative());
115-
$this->assertTrue($adjustment->isIncluded());
135+
$this->assertEquals('0.1', $adjustment->getPercentage());
116136
$this->assertEquals('1', $adjustment->getSourceId());
137+
$this->assertTrue($adjustment->isIncluded());
117138
}
118139

119140
}

modules/order/tests/src/Kernel/Entity/OrderItemTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public function testOrderItem() {
9696
'type' => 'custom',
9797
'label' => '10% off',
9898
'amount' => new Price('-1.00', 'USD'),
99+
'percentage' => '0.1',
99100
]);
100101
$adjustments[] = new Adjustment([
101102
'type' => 'custom',

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public function apply(EntityInterface $entity, PromotionInterface $promotion) {
2424
$this->assertEntity($entity);
2525
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
2626
$order_item = $entity;
27-
$adjustment_amount = $order_item->getUnitPrice()->multiply($this->getAmount());
27+
$adjustment_amount = $order_item->getUnitPrice()->multiply($this->getPercentage());
2828
$adjustment_amount = $this->rounder->round($adjustment_amount);
2929

3030
$order_item->addAdjustment(new Adjustment([
3131
'type' => 'promotion',
3232
// @todo Change to label from UI when added in #2770731.
3333
'label' => t('Discount'),
3434
'amount' => $adjustment_amount->multiply('-1'),
35+
'percentage' => $this->getPercentage(),
3536
'source_id' => $promotion->id(),
3637
]));
3738
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public function apply(EntityInterface $entity, PromotionInterface $promotion) {
2424
$this->assertEntity($entity);
2525
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
2626
$order = $entity;
27-
$adjustment_amount = $order->getTotalPrice()->multiply($this->getAmount());
27+
$adjustment_amount = $order->getTotalPrice()->multiply($this->getPercentage());
2828
$adjustment_amount = $this->rounder->round($adjustment_amount);
2929

3030
$order->addAdjustment(new Adjustment([
3131
'type' => 'promotion',
3232
// @todo Change to label from UI when added in #2770731.
3333
'label' => t('Discount'),
3434
'amount' => $adjustment_amount->multiply('-1'),
35+
'percentage' => $this->getPercentage(),
3536
'source_id' => $promotion->id(),
3637
]));
3738
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ abstract class PercentageOffBase extends PromotionOfferBase {
1414
*/
1515
public function defaultConfiguration() {
1616
return [
17-
'amount' => 0,
17+
'amount' => '0',
1818
] + parent::defaultConfiguration();
1919
}
2020

2121
/**
22-
* Gets the percentage amount.
22+
* Gets the percentage.
2323
*
2424
* @return string
25-
* The amount.
25+
* The percentage.
2626
*/
27-
public function getAmount() {
27+
public function getPercentage() {
2828
return (string) $this->configuration['amount'];
2929
}
3030

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public function testProductPercentageOff() {
238238
$adjustment = reset($adjustments);
239239
// Adjustment for 50% of the order item total.
240240
$this->assertEquals(new Price('-5.00', 'USD'), $adjustment->getAmount());
241+
$this->assertEquals('0.50', $adjustment->getPercentage());
241242
// Adjustments don't affect total order item price, but the order's total.
242243
$this->assertEquals(new Price('20.00', 'USD'), $order_item->getTotalPrice());
243244

0 commit comments

Comments
 (0)