Skip to content

Commit 4461889

Browse files
committed
MAGETWO-89990: Display error messages on success and review pages (MAGETWO-85819)
1 parent 5d5c3d3 commit 4461889

File tree

15 files changed

+1117
-200
lines changed

15 files changed

+1117
-200
lines changed

app/code/Magento/Multishipping/Block/Checkout/Overview.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ public function formatPrice($price)
196196

197197
/**
198198
* @param Address $address
199-
* @return mixed
199+
* @return array
200200
*/
201-
public function getShippingAddressItems($address)
201+
public function getShippingAddressItems($address): array
202202
{
203203
return $address->getAllVisibleItems();
204204
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Multishipping\Block\Checkout;
7+
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\View\Element\Template\Context;
10+
use Magento\Multishipping\Model\Checkout\Type\Multishipping;
11+
use Magento\Sales\Api\OrderRepositoryInterface;
12+
use Magento\Customer\Model\Address\Config as AddressConfig;
13+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
14+
use Magento\Sales\Model\Order\Address as OrderAddress;
15+
use Magento\Framework\Session\SessionManagerInterface;
16+
17+
/**
18+
* Multi-shipping checkout results information
19+
*
20+
* @api
21+
*/
22+
class Results extends Success
23+
{
24+
/**
25+
* @var AddressConfig
26+
*/
27+
private $addressConfig;
28+
29+
/**
30+
* @var OrderRepositoryInterface
31+
*/
32+
private $orderRepository;
33+
34+
/**
35+
* @var SessionManagerInterface
36+
*/
37+
private $session;
38+
39+
/**
40+
* @var Multishipping
41+
*/
42+
private $multishipping;
43+
44+
/**
45+
* @param Context $context
46+
* @param Multishipping $multishipping
47+
* @param AddressConfig $addressConfig
48+
* @param OrderRepositoryInterface $orderRepository
49+
* @param SessionManagerInterface $session
50+
* @param array $data
51+
*/
52+
public function __construct(
53+
Context $context,
54+
Multishipping $multishipping,
55+
AddressConfig $addressConfig,
56+
OrderRepositoryInterface $orderRepository,
57+
SessionManagerInterface $session,
58+
array $data = []
59+
) {
60+
parent::__construct($context, $multishipping, $data);
61+
62+
$this->multishipping = $multishipping;
63+
$this->addressConfig = $addressConfig;
64+
$this->orderRepository = $orderRepository;
65+
$this->session = $session;
66+
}
67+
68+
/**
69+
* Returns shipping addresses from quote.
70+
*
71+
* @return array
72+
*/
73+
public function getQuoteShippingAddresses(): array
74+
{
75+
return $this->multishipping->getQuote()->getAllShippingAddresses();
76+
}
77+
78+
/**
79+
* Returns all failed addresses from quote.
80+
*
81+
* @return array
82+
*/
83+
public function getFailedAddresses(): array
84+
{
85+
$addresses = $this->getQuoteShippingAddresses();
86+
if ($this->getAddressError($this->getQuoteBillingAddress())) {
87+
$addresses[] = $this->getQuoteBillingAddress();
88+
}
89+
return $addresses;
90+
}
91+
92+
/**
93+
* Retrieve order shipping address.
94+
*
95+
* @param int $orderId
96+
* @return OrderAddress|null
97+
*/
98+
public function getOrderShippingAddress(int $orderId)
99+
{
100+
return $this->orderRepository->get($orderId)->getShippingAddress();
101+
}
102+
103+
/**
104+
* Retrieve quote billing address.
105+
*
106+
* @return QuoteAddress
107+
*/
108+
public function getQuoteBillingAddress(): QuoteAddress
109+
{
110+
return $this->getCheckout()->getQuote()->getBillingAddress();
111+
}
112+
113+
/**
114+
* Returns formatted shipping address from placed order.
115+
*
116+
* @param OrderAddress $address
117+
* @return string
118+
*/
119+
public function formatOrderShippingAddress(OrderAddress $address): string
120+
{
121+
return $this->getAddressOneline($address->getData());
122+
}
123+
124+
/**
125+
* Returns formatted shipping address from quote.
126+
*
127+
* @param QuoteAddress $address
128+
* @return string
129+
*/
130+
public function formatQuoteShippingAddress(QuoteAddress $address): string
131+
{
132+
return $this->getAddressOneline($address->getData());
133+
}
134+
135+
/**
136+
* Checks if address type is shipping.
137+
*
138+
* @param QuoteAddress $address
139+
* @return bool
140+
*/
141+
public function isShippingAddress(QuoteAddress $address): bool
142+
{
143+
return $address->getAddressType() === QuoteAddress::ADDRESS_TYPE_SHIPPING;
144+
}
145+
146+
/**
147+
* Get unescaped address formatted as one line string.
148+
*
149+
* @param array $address
150+
* @return string
151+
*/
152+
private function getAddressOneline(array $address): string
153+
{
154+
$renderer = $this->addressConfig->getFormatByCode('oneline')->getRenderer();
155+
156+
return $renderer->renderArray($address);
157+
}
158+
159+
/**
160+
* Returns address error.
161+
*
162+
* @param QuoteAddress $address
163+
* @return string
164+
*/
165+
public function getAddressError(QuoteAddress $address): string
166+
{
167+
$errors = $this->session->getAddressErrors();
168+
169+
return $errors[$address->getId()] ?? '';
170+
}
171+
172+
/**
173+
* Add title to block head.
174+
*
175+
* @throws LocalizedException
176+
* @return Success
177+
*/
178+
protected function _prepareLayout(): Success
179+
{
180+
$pageTitle = $this->getLayout()->getBlock('page.main.title');
181+
if ($pageTitle) {
182+
$title = $this->getOrderIds() ? $pageTitle->getPartlySuccessTitle() : $pageTitle->getFailedTitle();
183+
$pageTitle->setPageTitle($title);
184+
}
185+
186+
return parent::_prepareLayout();
187+
}
188+
}

app/code/Magento/Multishipping/Block/DataProviders/Overview.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,34 @@ public function __construct(
4141
* @return string
4242
*/
4343
public function getAddressError(Address $address): string
44+
{
45+
$addressErrors = $this->getAddressErrors();
46+
47+
return $addressErrors[$address->getId()] ?? '';
48+
}
49+
50+
/**
51+
* Returns all stored errors.
52+
*
53+
* @return array
54+
*/
55+
public function getAddressErrors(): array
4456
{
4557
if (empty($this->addressErrors)) {
4658
$this->addressErrors = $this->session->getAddressErrors(true);
4759
}
4860

49-
return $this->addressErrors[$address->getId()] ?? '';
61+
return $this->addressErrors ?? [];
62+
}
63+
64+
/**
65+
* Creates anchor name for address Id.
66+
*
67+
* @param int $addressId
68+
* @return string
69+
*/
70+
public function getAddressAnchorName(int $addressId): string
71+
{
72+
return 'a' . $addressId;
5073
}
5174
}

app/code/Magento/Multishipping/Block/DataProviders/Success.php

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,13 @@
66

77
namespace Magento\Multishipping\Block\DataProviders;
88

9-
use Magento\Framework\Session\SessionManagerInterface;
109
use Magento\Framework\View\Element\Block\ArgumentInterface;
11-
use Magento\Customer\Model\Address\Config as AddressConfig;
12-
use Magento\Multishipping\Model\Checkout\Type\Multishipping;
13-
use Magento\Quote\Model\Quote;
14-
use Magento\Quote\Model\Quote\Address;
10+
use Magento\Multishipping\Block\Checkout\Results;
1511

1612
/**
1713
* Provides additional data for multishipping checkout success step.
1814
*/
19-
class Success implements ArgumentInterface
15+
class Success extends Results implements ArgumentInterface
2016
{
21-
/**
22-
* @var SessionManagerInterface
23-
*/
24-
private $session;
2517

26-
/**
27-
* @var Multishipping
28-
*/
29-
private $multishipping;
30-
31-
/**
32-
* @var AddressConfig
33-
*/
34-
private $addressConfig;
35-
36-
/**
37-
* @param SessionManagerInterface $session
38-
* @param Multishipping $multishipping
39-
* @param AddressConfig $addressConfig
40-
*/
41-
public function __construct(
42-
SessionManagerInterface $session,
43-
Multishipping $multishipping,
44-
AddressConfig $addressConfig
45-
) {
46-
$this->session = $session;
47-
$this->multishipping = $multishipping;
48-
$this->addressConfig = $addressConfig;
49-
}
50-
51-
/**
52-
* Returns shipping addresses from quote.
53-
*
54-
* @return array
55-
*/
56-
public function getShippingAddresses(): array
57-
{
58-
return $this->multishipping->getQuote()->getAllShippingAddresses();
59-
}
60-
61-
/**
62-
* Returns quote.
63-
*
64-
* @return Quote
65-
*/
66-
public function getQuote(): Quote
67-
{
68-
return $this->multishipping->getQuote();
69-
}
70-
71-
/**
72-
* Returns address error.
73-
*
74-
* @param Address $address
75-
* @return string
76-
*/
77-
public function getAddressError(Address $address): string
78-
{
79-
$errors = $this->session->getAddressErrors();
80-
81-
return $errors[$address->getId()] ?? '';
82-
}
83-
84-
/**
85-
* Get address formatted as html string.
86-
*
87-
* @param Address $address
88-
* @return string
89-
*/
90-
public function getAddressHtml(Address $address): string
91-
{
92-
$renderer = $this->addressConfig->getFormatByCode('html')->getRenderer();
93-
94-
return $renderer->renderArray($address->getData());
95-
}
9618
}

app/code/Magento/Multishipping/Controller/Checkout/OverviewPost.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Customer\Api\AccountManagementInterface;
1010
use Magento\Customer\Api\CustomerRepositoryInterface;
1111
use Magento\Framework\Exception\PaymentException;
12+
use Magento\Framework\Session\SessionManagerInterface;
1213

1314
/**
1415
* Class OverviewPost
@@ -32,6 +33,11 @@ class OverviewPost extends \Magento\Multishipping\Controller\Checkout
3233
*/
3334
protected $agreementsValidator;
3435

36+
/**
37+
* @var SessionManagerInterface
38+
*/
39+
private $session;
40+
3541
/**
3642
* @param \Magento\Framework\App\Action\Context $context
3743
* @param \Magento\Customer\Model\Session $customerSession
@@ -40,6 +46,7 @@ class OverviewPost extends \Magento\Multishipping\Controller\Checkout
4046
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
4147
* @param \Psr\Log\LoggerInterface $logger
4248
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator
49+
* @param SessionManagerInterface $session
4350
*/
4451
public function __construct(
4552
\Magento\Framework\App\Action\Context $context,
@@ -48,11 +55,14 @@ public function __construct(
4855
AccountManagementInterface $accountManagement,
4956
\Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
5057
\Psr\Log\LoggerInterface $logger,
51-
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator
58+
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator,
59+
SessionManagerInterface $session
5260
) {
5361
$this->formKeyValidator = $formKeyValidator;
5462
$this->logger = $logger;
5563
$this->agreementsValidator = $agreementValidator;
64+
$this->session = $session;
65+
5666
parent::__construct(
5767
$context,
5868
$customerSession,
@@ -95,11 +105,17 @@ public function execute()
95105
$paymentInstance->setCcCid($payment['cc_cid']);
96106
}
97107
$this->_getCheckout()->createOrders();
98-
$this->_getState()->setActiveStep(State::STEP_SUCCESS);
99108
$this->_getState()->setCompleteStep(State::STEP_OVERVIEW);
100-
$this->_getCheckout()->getCheckoutSession()->clearQuote();
101-
$this->_getCheckout()->getCheckoutSession()->setDisplaySuccess(true);
102-
$this->_redirect('*/*/success');
109+
110+
if ($this->session->getAddressErrors()) {
111+
$this->_getState()->setActiveStep(State::STEP_RESULTS);
112+
$this->_redirect('*/*/results');
113+
} else {
114+
$this->_getState()->setActiveStep(State::STEP_SUCCESS);
115+
$this->_getCheckout()->getCheckoutSession()->clearQuote();
116+
$this->_getCheckout()->getCheckoutSession()->setDisplaySuccess(true);
117+
$this->_redirect('*/*/success');
118+
}
103119
} catch (PaymentException $e) {
104120
$message = $e->getMessage();
105121
if (!empty($message)) {

0 commit comments

Comments
 (0)