Skip to content

Commit af70540

Browse files
author
Joan He
committed
Merge remote-tracking branch 'arcticfoxes/MAGETWO-91678' into BugFixPR
2 parents 58092d2 + eab6378 commit af70540

18 files changed

+856
-218
lines changed

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

Lines changed: 20 additions & 40 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,50 +84,20 @@ 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 $validationResult) {
88+
if ($validationResult->isValid()) {
89+
continue;
8590
}
8691

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+
$messages = $validationResult->getErrors();
93+
$defaultMessage = array_shift($messages);
94+
if ($defaultMessage && !empty($messages)) {
95+
$defaultMessage .= ' %1';
9596
}
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-
);
97+
if ($defaultMessage) {
98+
throw new LocalizedException(__($defaultMessage, implode(' ', $messages)));
10399
}
104100
}
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());
120-
}
121101

122102
return $this;
123103
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Framework\Validation\ValidationResultFactory;
12+
use Magento\Quote\Model\Quote;
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
class AllowedCountryValidationRule implements QuoteValidationRuleInterface
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $generalMessage;
23+
24+
/**
25+
* @var AllowedCountries
26+
*/
27+
private $allowedCountryReader;
28+
29+
/**
30+
* @var ValidationResultFactory
31+
*/
32+
private $validationResultFactory;
33+
34+
/**
35+
* @param AllowedCountries $allowedCountryReader
36+
* @param ValidationResultFactory $validationResultFactory
37+
* @param string $generalMessage
38+
*/
39+
public function __construct(
40+
AllowedCountries $allowedCountryReader,
41+
ValidationResultFactory $validationResultFactory,
42+
string $generalMessage = ''
43+
) {
44+
$this->allowedCountryReader = $allowedCountryReader;
45+
$this->validationResultFactory = $validationResultFactory;
46+
$this->generalMessage = $generalMessage;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function validate(Quote $quote): array
53+
{
54+
$validationErrors = [];
55+
56+
if (!$quote->isVirtual()) {
57+
$validationResult =
58+
in_array(
59+
$quote->getShippingAddress()->getCountryId(),
60+
$this->allowedCountryReader->getAllowedCountries()
61+
);
62+
if (!$validationResult) {
63+
$validationErrors = [__($this->generalMessage)];
64+
}
65+
}
66+
67+
return [$this->validationResultFactory->create(['errors' => $validationErrors])];
68+
}
69+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Framework\Validation\ValidationResultFactory;
11+
use Magento\Quote\Model\Quote;
12+
13+
/**
14+
* @inheritdoc
15+
*/
16+
class BillingAddressValidationRule implements QuoteValidationRuleInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $generalMessage;
22+
23+
/**
24+
* @var ValidationResultFactory
25+
*/
26+
private $validationResultFactory;
27+
28+
/**
29+
* @param ValidationResultFactory $validationResultFactory
30+
* @param string $generalMessage
31+
*/
32+
public function __construct(
33+
ValidationResultFactory $validationResultFactory,
34+
string $generalMessage = ''
35+
) {
36+
$this->validationResultFactory = $validationResultFactory;
37+
$this->generalMessage = $generalMessage;
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function validate(Quote $quote): array
44+
{
45+
$validationErrors = [];
46+
$validationResult = $quote->getBillingAddress()->validate();
47+
if ($validationResult !== true) {
48+
$validationErrors = [__($this->generalMessage)];
49+
}
50+
if (is_array($validationResult)) {
51+
$validationErrors = array_merge($validationErrors, $validationResult);
52+
}
53+
54+
return [$this->validationResultFactory->create(['errors' => $validationErrors])];
55+
}
56+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Framework\Validation\ValidationResultFactory;
11+
use Magento\Quote\Model\Quote;
12+
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage;
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
class MinimumAmountValidationRule implements QuoteValidationRuleInterface
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $generalMessage;
23+
24+
/**
25+
* @var ValidationMessage
26+
*/
27+
private $amountValidationMessage;
28+
29+
/**
30+
* @var ValidationResultFactory
31+
*/
32+
private $validationResultFactory;
33+
34+
/**
35+
* @param ValidationMessage $amountValidationMessage
36+
* @param ValidationResultFactory $validationResultFactory
37+
* @param string $generalMessage
38+
*/
39+
public function __construct(
40+
ValidationMessage $amountValidationMessage,
41+
ValidationResultFactory $validationResultFactory,
42+
string $generalMessage = ''
43+
) {
44+
$this->amountValidationMessage = $amountValidationMessage;
45+
$this->validationResultFactory = $validationResultFactory;
46+
$this->generalMessage = $generalMessage;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
* @throws \Zend_Currency_Exception
52+
*/
53+
public function validate(Quote $quote): array
54+
{
55+
$validationErrors = [];
56+
$validationResult = $quote->validateMinimumAmount($quote->getIsMultiShipping());
57+
if (!$validationResult) {
58+
if (!$this->generalMessage) {
59+
$this->generalMessage = $this->amountValidationMessage->getMessage();
60+
}
61+
$validationErrors = [__($this->generalMessage)];
62+
}
63+
64+
return [$this->validationResultFactory->create(['errors' => $validationErrors])];
65+
}
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Framework\Validation\ValidationResultFactory;
11+
use Magento\Quote\Model\Quote;
12+
13+
/**
14+
* @inheritdoc
15+
*/
16+
class PaymentMethodValidationRule implements QuoteValidationRuleInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $generalMessage;
22+
23+
/**
24+
* @var ValidationResultFactory
25+
*/
26+
private $validationResultFactory;
27+
28+
/**
29+
* @param ValidationResultFactory $validationResultFactory
30+
* @param string $generalMessage
31+
*/
32+
public function __construct(
33+
ValidationResultFactory $validationResultFactory,
34+
string $generalMessage = ''
35+
) {
36+
$this->validationResultFactory = $validationResultFactory;
37+
$this->generalMessage = $generalMessage;
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function validate(Quote $quote): array
44+
{
45+
$validationErrors = [];
46+
$validationResult = $quote->getPayment()->getMethod();
47+
if (!$validationResult) {
48+
$validationErrors = [__($this->generalMessage)];
49+
}
50+
51+
return [$this->validationResultFactory->create(['errors' => $validationErrors])];
52+
}
53+
}

0 commit comments

Comments
 (0)