Skip to content

Commit b58eb36

Browse files
committed
MAGETWO-91678: Minimum Order amount required in Admin orders
1 parent 11ade1f commit b58eb36

11 files changed

+426
-42
lines changed

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

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Directory\Model\AllowedCountries;
1212
use Magento\Framework\App\ObjectManager;
1313
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage as OrderAmountValidationMessage;
14+
use Magento\Quote\Model\ValidationRules\QuoteValidationRuleInterface;
1415

1516
/**
1617
* @api
@@ -33,20 +34,29 @@ class QuoteValidator
3334
*/
3435
private $minimumAmountMessage;
3536

37+
/**
38+
* @var QuoteValidationRuleInterface
39+
*/
40+
private $quoteValidationRule;
41+
3642
/**
3743
* QuoteValidator constructor.
3844
*
3945
* @param AllowedCountries|null $allowedCountryReader
4046
* @param OrderAmountValidationMessage|null $minimumAmountMessage
47+
* @param QuoteValidationRuleInterface|null $quoteValidationRule
4148
*/
4249
public function __construct(
4350
AllowedCountries $allowedCountryReader = null,
44-
OrderAmountValidationMessage $minimumAmountMessage = null
51+
OrderAmountValidationMessage $minimumAmountMessage = null,
52+
QuoteValidationRuleInterface $quoteValidationRule = null
4553
) {
4654
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
4755
->get(AllowedCountries::class);
4856
$this->minimumAmountMessage = $minimumAmountMessage ?: ObjectManager::getInstance()
4957
->get(OrderAmountValidationMessage::class);
58+
$this->quoteValidationRule = $quoteValidationRule ?: ObjectManager::getInstance()
59+
->get(QuoteValidationRuleInterface::class);
5060
}
5161

5262
/**
@@ -74,49 +84,14 @@ public function validateQuoteAmount(QuoteEntity $quote, $amount)
7484
*/
7585
public function validateBeforeSubmit(QuoteEntity $quote)
7686
{
77-
if (!$quote->isVirtual()) {
78-
if ($quote->getShippingAddress()->validate() !== true) {
79-
throw new \Magento\Framework\Exception\LocalizedException(
80-
__(
81-
'Please check the shipping address information. %1',
82-
implode(' ', $quote->getShippingAddress()->validate())
83-
)
84-
);
87+
foreach ($this->quoteValidationRule->validate($quote) as $messages) {
88+
$defaultMessage = array_shift($messages);
89+
if ($defaultMessage && !empty($messages)) {
90+
$defaultMessage .= ' %1';
8591
}
86-
87-
// Checks if country id present in the allowed countries list.
88-
if (!in_array(
89-
$quote->getShippingAddress()->getCountryId(),
90-
$this->allowedCountryReader->getAllowedCountries()
91-
)) {
92-
throw new \Magento\Framework\Exception\LocalizedException(
93-
__("Some addresses can't be used due to the configurations for specific countries.")
94-
);
92+
if ($defaultMessage) {
93+
throw new LocalizedException(__($defaultMessage, implode(' ', $messages)));
9594
}
96-
97-
$method = $quote->getShippingAddress()->getShippingMethod();
98-
$rate = $quote->getShippingAddress()->getShippingRateByCode($method);
99-
if (!$method || !$rate) {
100-
throw new \Magento\Framework\Exception\LocalizedException(
101-
__('The shipping method is missing. Select the shipping method and try again.')
102-
);
103-
}
104-
}
105-
if ($quote->getBillingAddress()->validate() !== true) {
106-
throw new \Magento\Framework\Exception\LocalizedException(
107-
__(
108-
'Please check the billing address information. %1',
109-
implode(' ', $quote->getBillingAddress()->validate())
110-
)
111-
);
112-
}
113-
if (!$quote->getPayment()->getMethod()) {
114-
throw new \Magento\Framework\Exception\LocalizedException(
115-
__('Enter a valid payment method and try again. ')
116-
);
117-
}
118-
if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
119-
throw new LocalizedException($this->minimumAmountMessage->getMessage());
12095
}
12196

12297
return $this;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\ValidationRules;
9+
10+
use Magento\Directory\Model\AllowedCountries;
11+
use Magento\Quote\Model\Quote;
12+
13+
class AllowedCountryValidationRule implements QuoteValidationRuleInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $defaultMessage;
19+
20+
/**
21+
* @var AllowedCountries
22+
*/
23+
private $allowedCountryReader;
24+
25+
/**
26+
* @param AllowedCountries $allowedCountryReader
27+
* @param string $defaultMessage
28+
*/
29+
public function __construct(AllowedCountries $allowedCountryReader, string $defaultMessage = '')
30+
{
31+
$this->defaultMessage = $defaultMessage;
32+
$this->allowedCountryReader = $allowedCountryReader;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function validate(Quote $quote): array
39+
{
40+
$validationErrors = [];
41+
42+
if (!$quote->isVirtual()) {
43+
$validationResult =
44+
in_array(
45+
$quote->getShippingAddress()->getCountryId(),
46+
$this->allowedCountryReader->getAllowedCountries()
47+
);
48+
if (!$validationResult) {
49+
$validationErrors = [$this->defaultMessage];
50+
}
51+
}
52+
53+
return $validationErrors ? [get_class($this) => $validationErrors] : [];
54+
}
55+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\ValidationRules;
9+
10+
use Magento\Quote\Model\Quote;
11+
12+
class BillingAddressValidationRule implements QuoteValidationRuleInterface
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $defaultMessage;
18+
19+
/**
20+
* @param string $defaultMessage
21+
*/
22+
public function __construct(string $defaultMessage = '')
23+
{
24+
$this->defaultMessage = $defaultMessage;
25+
}
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function validate(Quote $quote): array
31+
{
32+
$validationErrors = [];
33+
$validationResult = $quote->getBillingAddress()->validate();
34+
if ($validationResult !== true) {
35+
$validationErrors = [$this->defaultMessage];
36+
}
37+
if (is_array($validationResult)) {
38+
$validationErrors = array_merge($validationErrors, $validationResult);
39+
}
40+
41+
return $validationErrors ? [get_class($this) => $validationErrors] : [];
42+
}
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\ValidationRules;
9+
10+
use Magento\Quote\Model\Quote;
11+
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage;
12+
13+
class MinimumAmountValidationRule implements QuoteValidationRuleInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $defaultMessage;
19+
20+
/**
21+
* @var ValidationMessage
22+
*/
23+
private $amountValidationMessage;
24+
25+
/**
26+
* @param ValidationMessage $amountValidationMessage
27+
* @param string $defaultMessage
28+
*/
29+
public function __construct(ValidationMessage $amountValidationMessage, string $defaultMessage = '')
30+
{
31+
$this->amountValidationMessage = $amountValidationMessage;
32+
$this->defaultMessage = $defaultMessage;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
* @throws \Zend_Currency_Exception
38+
*/
39+
public function validate(Quote $quote): array
40+
{
41+
$validationErrors = [];
42+
$validationResult = $quote->validateMinimumAmount($quote->getIsMultiShipping());
43+
if (!$validationResult) {
44+
if (!$this->defaultMessage) {
45+
$this->defaultMessage = $this->amountValidationMessage->getMessage();
46+
}
47+
$validationErrors = [$this->defaultMessage];
48+
}
49+
50+
return $validationErrors ? [get_class($this) => $validationErrors] : [];
51+
}
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\ValidationRules;
9+
10+
use Magento\Quote\Model\Quote;
11+
12+
class PaymentMethodValidationRule implements QuoteValidationRuleInterface
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $defaultMessage;
18+
19+
/**
20+
* @param string $defaultMessage
21+
*/
22+
public function __construct(string $defaultMessage = '')
23+
{
24+
$this->defaultMessage = $defaultMessage;
25+
}
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function validate(Quote $quote): array
31+
{
32+
$validationErrors = [];
33+
$validationResult = $quote->getPayment()->getMethod();
34+
if (!$validationResult) {
35+
$validationErrors = [$this->defaultMessage];
36+
}
37+
38+
return $validationErrors ? [get_class($this) => $validationErrors] : [];
39+
}
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\ValidationRules;
9+
10+
use Magento\Quote\Model\Quote;
11+
12+
class QuoteValidationComposite implements QuoteValidationRuleInterface
13+
{
14+
/**
15+
* @var QuoteValidationRuleInterface[]
16+
*/
17+
private $validationRules = [];
18+
19+
public function __construct(array $validationRules)
20+
{
21+
$this->validationRules = $validationRules;
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function validate(Quote $quote): array
28+
{
29+
$aggregateResult = [];
30+
31+
foreach ($this->validationRules as $validationRule) {
32+
$ruleValidationResult = $validationRule->validate($quote);
33+
$aggregateResult += $ruleValidationResult;
34+
}
35+
36+
return $aggregateResult;
37+
}
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Quote\Model\ValidationRules;
8+
9+
use Magento\Quote\Model\Quote;
10+
11+
interface QuoteValidationRuleInterface
12+
{
13+
/**
14+
* Validate quote model.
15+
*
16+
* @param Quote $quote
17+
* @return array
18+
* [
19+
* 'ruleId_1' => [
20+
* 'Base error message',
21+
* 'Additional error message #1',
22+
* 'Additional error message #2',
23+
* 'Additional error message #3',
24+
* 'Additional error message #4',
25+
* ],
26+
* 'ruleId_2' => [
27+
* 'Base error message',
28+
* ]
29+
* ]
30+
*/
31+
public function validate(Quote $quote): array;
32+
}

0 commit comments

Comments
 (0)