Skip to content

Commit 77d2a6c

Browse files
committed
Added option to exclude discount for minimum order amount calculation
1 parent 6481cec commit 77d2a6c

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

app/code/Magento/Quote/Model/Quote.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,11 @@ public function validateMinimumAmount($multishipping = false)
22462246
if (!$minOrderActive) {
22472247
return true;
22482248
}
2249+
$includeDiscount = $this->_scopeConfig->getValue(
2250+
'sales/minimum_order/include_discount_amount',
2251+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
2252+
$storeId
2253+
);
22492254
$minOrderMulti = $this->_scopeConfig->isSetFlag(
22502255
'sales/minimum_order/multi_address',
22512256
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
@@ -2279,7 +2284,11 @@ public function validateMinimumAmount($multishipping = false)
22792284
$taxes = ($taxInclude) ? $address->getBaseTaxAmount() : 0;
22802285
foreach ($address->getQuote()->getItemsCollection() as $item) {
22812286
/** @var \Magento\Quote\Model\Quote\Item $item */
2282-
$amount = $item->getBaseRowTotal() - $item->getBaseDiscountAmount() + $taxes;
2287+
if ($includeDiscount) {
2288+
$amount = $item->getBaseRowTotal() - $item->getBaseDiscountAmount() + $taxes;
2289+
} else {
2290+
$amount = $taxInclude ? $item->getBaseRowTotalInclTax() : $item->getBaseRowTotal();
2291+
}
22832292
if ($amount < $minAmount) {
22842293
return false;
22852294
}
@@ -2289,7 +2298,11 @@ public function validateMinimumAmount($multishipping = false)
22892298
$baseTotal = 0;
22902299
foreach ($addresses as $address) {
22912300
$taxes = ($taxInclude) ? $address->getBaseTaxAmount() : 0;
2292-
$baseTotal += $address->getBaseSubtotalWithDiscount() + $taxes;
2301+
if ($includeDiscount) {
2302+
$baseTotal += $address->getBaseSubtotalWithDiscount() + $taxes;
2303+
} else {
2304+
$baseTotal += $taxInclude ? $address->getBaseSubtotalTotalInclTax() : $address->getBaseSubtotal();
2305+
}
22932306
}
22942307
if ($baseTotal < $minAmount) {
22952308
return false;

app/code/Magento/Quote/Model/Quote/Address.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,11 @@ public function validateMinimumAmount()
11431143
return true;
11441144
}
11451145

1146+
$includeDiscount = $this->_scopeConfig->getValue(
1147+
'sales/minimum_order/include_discount_amount',
1148+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
1149+
$storeId
1150+
);
11461151
$amount = $this->_scopeConfig->getValue(
11471152
'sales/minimum_order/amount',
11481153
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
@@ -1153,9 +1158,18 @@ public function validateMinimumAmount()
11531158
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
11541159
$storeId
11551160
);
1156-
$taxes = $taxInclude ? $this->getBaseTaxAmount() : 0;
11571161

1158-
return ($this->getBaseSubtotalWithDiscount() + $taxes >= $amount);
1162+
if ($includeDiscount) {
1163+
$taxes = $taxInclude ? $this->getBaseTaxAmount() : 0;
1164+
1165+
$isMinimumReached = ($this->getBaseSubtotalWithDiscount() + $taxes >= $amount);
1166+
} else {
1167+
$isMinimumReached = $taxInclude
1168+
? ($this->getBaseSubtotalTotalInclTax() >= $amount)
1169+
: ($this->getBaseSubtotal() >= $amount);
1170+
}
1171+
1172+
return $isMinimumReached;
11591173
}
11601174

11611175
/**

app/code/Magento/Quote/Test/Unit/Model/Quote/AddressTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public function testValidateMinimumAmountVirtual()
216216
$scopeConfigValues = [
217217
['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
218218
['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
219+
['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
219220
['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
220221
];
221222

@@ -240,6 +241,31 @@ public function testValidateMinimumAmount()
240241
$scopeConfigValues = [
241242
['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
242243
['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
244+
['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
245+
['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
246+
];
247+
248+
$this->quote->expects($this->once())
249+
->method('getStoreId')
250+
->willReturn($storeId);
251+
$this->quote->expects($this->once())
252+
->method('getIsVirtual')
253+
->willReturn(false);
254+
255+
$this->scopeConfig->expects($this->once())
256+
->method('isSetFlag')
257+
->willReturnMap($scopeConfigValues);
258+
259+
$this->assertTrue($this->address->validateMinimumAmount());
260+
}
261+
262+
public function testValidateMiniumumAmountWithoutDiscount()
263+
{
264+
$storeId = 1;
265+
$scopeConfigValues = [
266+
['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
267+
['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
268+
['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, false],
243269
['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
244270
];
245271

@@ -263,6 +289,7 @@ public function testValidateMinimumAmountNegative()
263289
$scopeConfigValues = [
264290
['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
265291
['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
292+
['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
266293
['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
267294
];
268295

app/code/Magento/Quote/Test/Unit/Model/QuoteTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ public function testValidateMinimumAmount()
975975
['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
976976
['sales/minimum_order/multi_address', ScopeInterface::SCOPE_STORE, $storeId, true],
977977
['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
978+
['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
978979
['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
979980
];
980981
$this->scopeConfig->expects($this->any())
@@ -1001,6 +1002,7 @@ public function testValidateMinimumAmountNegative()
10011002
['sales/minimum_order/active', ScopeInterface::SCOPE_STORE, $storeId, true],
10021003
['sales/minimum_order/multi_address', ScopeInterface::SCOPE_STORE, $storeId, true],
10031004
['sales/minimum_order/amount', ScopeInterface::SCOPE_STORE, $storeId, 20],
1005+
['sales/minimum_order/include_discount_amount', ScopeInterface::SCOPE_STORE, $storeId, true],
10041006
['sales/minimum_order/tax_including', ScopeInterface::SCOPE_STORE, $storeId, true],
10051007
];
10061008
$this->scopeConfig->expects($this->any())

app/code/Magento/Sales/etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@
8989
<label>Minimum Amount</label>
9090
<comment>Subtotal after discount</comment>
9191
</field>
92+
<field id="include_discount_amount" translate="label" sortOrder="12" type="select" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
93+
<label>Include Discount Amount</label>
94+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
95+
<comment>Choosing yes will be used subtotal after discount, otherwise only subtotal will be used</comment>
96+
</field>
9297
<field id="tax_including" translate="label" sortOrder="15" type="select" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
9398
<label>Include Tax to Amount</label>
9499
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>

app/code/Magento/Sales/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<allow_zero_grandtotal>1</allow_zero_grandtotal>
2323
</zerograndtotal_creditmemo>
2424
<minimum_order>
25+
<include_discount_amount>1</include_discount_amount>
2526
<tax_including>1</tax_including>
2627
</minimum_order>
2728
<orders>

0 commit comments

Comments
 (0)