Skip to content

Commit 89622ea

Browse files
author
Alexander Akimov
authored
Merge pull request #3165 from magento-tsg/2.2-develop-pr44
[TSG] Backporting for 2.2 (pr44) (2.2.7)
2 parents 91e599c + 14587ed commit 89622ea

36 files changed

+1295
-315
lines changed

app/code/Magento/Dhl/Model/Carrier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1970,6 +1970,6 @@ protected function isDutiable($origCountryId, $destCountryId)
19701970

19711971
return
19721972
self::DHL_CONTENT_TYPE_NON_DOC == $this->getConfigData('content_type')
1973-
&& !$this->_isDomestic;
1973+
|| !$this->_isDomestic;
19741974
}
19751975
}

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

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Framework\Message\Error;
1313
use Magento\Quote\Model\Quote as QuoteEntity;
1414
use Magento\Quote\Model\Quote\Validator\MinimumOrderAmount\ValidationMessage as OrderAmountValidationMessage;
15-
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Quote\Model\ValidationRules\QuoteValidationRuleInterface;
1616

1717
/**
1818
* @api
@@ -35,20 +35,29 @@ class QuoteValidator
3535
*/
3636
private $minimumAmountMessage;
3737

38+
/**
39+
* @var QuoteValidationRuleInterface
40+
*/
41+
private $quoteValidationRule;
42+
3843
/**
3944
* QuoteValidator constructor.
4045
*
4146
* @param AllowedCountries|null $allowedCountryReader
4247
* @param OrderAmountValidationMessage|null $minimumAmountMessage
48+
* @param QuoteValidationRuleInterface|null $quoteValidationRule
4349
*/
4450
public function __construct(
4551
AllowedCountries $allowedCountryReader = null,
46-
OrderAmountValidationMessage $minimumAmountMessage = null
52+
OrderAmountValidationMessage $minimumAmountMessage = null,
53+
QuoteValidationRuleInterface $quoteValidationRule = null
4754
) {
4855
$this->allowedCountryReader = $allowedCountryReader ?: ObjectManager::getInstance()
4956
->get(AllowedCountries::class);
5057
$this->minimumAmountMessage = $minimumAmountMessage ?: ObjectManager::getInstance()
5158
->get(OrderAmountValidationMessage::class);
59+
$this->quoteValidationRule = $quoteValidationRule ?: ObjectManager::getInstance()
60+
->get(QuoteValidationRuleInterface::class);
5261
}
5362

5463
/**
@@ -82,66 +91,24 @@ public function validateBeforeSubmit(QuoteEntity $quote)
8291
throw new LocalizedException(__($errors ?: 'Something went wrong. Please try to place the order again.'));
8392
}
8493

85-
if (!$quote->isVirtual()) {
86-
$this->validateShippingAddress($quote);
87-
}
88-
89-
$billingAddress = $quote->getBillingAddress();
90-
$billingAddress->setStoreId($quote->getStoreId());
91-
if ($billingAddress->validate() !== true) {
92-
throw new LocalizedException(
93-
__(
94-
'Please check the billing address information. %1',
95-
implode(' ', $quote->getBillingAddress()->validate())
96-
)
97-
);
98-
}
99-
if (!$quote->getPayment()->getMethod()) {
100-
throw new LocalizedException(__('Please select a valid payment method.'));
101-
}
102-
if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
103-
throw new LocalizedException($this->minimumAmountMessage->getMessage());
94+
foreach ($this->quoteValidationRule->validate($quote) as $validationResult) {
95+
if ($validationResult->isValid()) {
96+
continue;
97+
}
98+
99+
$messages = $validationResult->getErrors();
100+
$defaultMessage = array_shift($messages);
101+
if ($defaultMessage && !empty($messages)) {
102+
$defaultMessage .= ' %1';
103+
}
104+
if ($defaultMessage) {
105+
throw new LocalizedException(__($defaultMessage, implode(' ', $messages)));
106+
}
104107
}
105108

106109
return $this;
107110
}
108111

109-
/**
110-
* Validates shipping address.
111-
*
112-
* @param Quote $quote
113-
* @throws LocalizedException
114-
*/
115-
private function validateShippingAddress(QuoteEntity $quote)
116-
{
117-
$address = $quote->getShippingAddress();
118-
$address->setStoreId($quote->getStoreId());
119-
if ($address->validate() !== true) {
120-
throw new LocalizedException(
121-
__(
122-
'Please check the shipping address information. %1',
123-
implode(' ', $address->validate())
124-
)
125-
);
126-
}
127-
128-
// Checks if country id present in the allowed countries list.
129-
if (!in_array(
130-
$address->getCountryId(),
131-
$this->allowedCountryReader->getAllowedCountries(ScopeInterface::SCOPE_STORE, $quote->getStoreId())
132-
)) {
133-
throw new LocalizedException(
134-
__('Some addresses cannot be used due to country-specific configurations.')
135-
);
136-
}
137-
138-
$method = $address->getShippingMethod();
139-
$rate = $address->getShippingRateByCode($method);
140-
if (!$method || !$rate) {
141-
throw new LocalizedException(__('Please specify a shipping method.'));
142-
}
143-
}
144-
145112
/**
146113
* Parses quote error messages and concatenates them into single string.
147114
*
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)