Skip to content

Commit c956f29

Browse files
committed
ACP2E-341::Product is not getting added in wishlist from product list page and product view page when customer confirms account from confirmation email - Fixed Automation Tests
1 parent a8543fe commit c956f29

File tree

5 files changed

+381
-13
lines changed

5 files changed

+381
-13
lines changed

app/code/Magento/Customer/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,4 +567,7 @@
567567
</argument>
568568
</arguments>
569569
</type>
570+
<type name="Magento\Customer\Model\EmailNotificationInterface">
571+
<plugin name="saveWishlistDataAndAddReferenceKeyToBackUrl" type="Magento\Wishlist\Plugin\SaveWishlistDataAndAddReferenceKeyToBackUrl"/>
572+
</type>
570573
</config>

app/code/Magento/Wishlist/Controller/Index/Add.php

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
use Magento\Framework\App\Response\RedirectInterface;
2121
use Magento\Framework\Controller\ResultInterface;
2222
use Magento\Wishlist\Controller\WishlistProviderInterface;
23+
use Magento\Framework\App\Action\HttpGetActionInterface;
24+
use Magento\Wishlist\Model\DataSerializer;
25+
use Magento\Framework\Data\Form\FormKey;
2326

2427
/**
2528
* Wish list Add controller
2629
*
2730
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2831
*/
29-
class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPostActionInterface
32+
class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPostActionInterface, HttpGetActionInterface
3033
{
3134
/**
3235
* @var WishlistProviderInterface
@@ -58,6 +61,16 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPost
5861
*/
5962
private $urlBuilder;
6063

64+
/**
65+
* @var DataSerializer
66+
*/
67+
private $dataSerializer;
68+
69+
/**
70+
* @var FormKey
71+
*/
72+
private $formKey;
73+
6174
/**
6275
* @param Context $context
6376
* @param Session $customerSession
@@ -66,22 +79,28 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPost
6679
* @param Validator $formKeyValidator
6780
* @param RedirectInterface|null $redirect
6881
* @param UrlInterface|null $urlBuilder
82+
* @param DataSerializer|null $dataSerializer
83+
* @param FormKey|null $formKey
6984
*/
7085
public function __construct(
7186
Context $context,
7287
Session $customerSession,
7388
WishlistProviderInterface $wishlistProvider,
7489
ProductRepositoryInterface $productRepository,
7590
Validator $formKeyValidator,
76-
RedirectInterface $redirect = null,
77-
UrlInterface $urlBuilder = null
91+
?RedirectInterface $redirect = null,
92+
?UrlInterface $urlBuilder = null,
93+
?DataSerializer $dataSerializer = null,
94+
?FormKey $formKey = null
7895
) {
7996
$this->_customerSession = $customerSession;
8097
$this->wishlistProvider = $wishlistProvider;
8198
$this->productRepository = $productRepository;
8299
$this->formKeyValidator = $formKeyValidator;
83100
$this->redirect = $redirect ?: ObjectManager::getInstance()->get(RedirectInterface::class);
84101
$this->urlBuilder = $urlBuilder ?: ObjectManager::getInstance()->get(UrlInterface::class);
102+
$this->dataSerializer = $dataSerializer ?: ObjectManager::getInstance()->get(DataSerializer::class);
103+
$this->formKey = $formKey ?: ObjectManager::getInstance()->get(FormKey::class);
85104
parent::__construct($context);
86105
}
87106

@@ -93,11 +112,28 @@ public function __construct(
93112
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
94113
* @SuppressWarnings(PHPMD.NPathComplexity)
95114
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
115+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
96116
*/
97117
public function execute()
98118
{
99119
/** @var Redirect $resultRedirect */
100120
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
121+
$session = $this->_customerSession;
122+
$requestParams = $this->getRequest()->getParams();
123+
124+
if ($session->getBeforeWishlistRequest()) {
125+
$requestParams = $session->getBeforeWishlistRequest();
126+
$session->unsBeforeWishlistRequest();
127+
$this->getRequest()->setParam('form_key', $requestParams['form_key']);
128+
}
129+
130+
if (isset($requestParams['token'])) {
131+
$wishlistRequestBeforeLogin = $this->dataSerializer->unserialize($requestParams['token']);
132+
$requestParams['product'] = isset($wishlistRequestBeforeLogin['product']) ?
133+
(int)$wishlistRequestBeforeLogin['product'] : null;
134+
$this->getRequest()->setParam('form_key', $this->formKey->getFormKey());
135+
}
136+
101137
if (!$this->formKeyValidator->validate($this->getRequest())) {
102138
return $resultRedirect->setPath('*/');
103139
}
@@ -107,15 +143,6 @@ public function execute()
107143
throw new NotFoundException(__('Page not found.'));
108144
}
109145

110-
$session = $this->_customerSession;
111-
112-
$requestParams = $this->getRequest()->getParams();
113-
114-
if ($session->getBeforeWishlistRequest()) {
115-
$requestParams = $session->getBeforeWishlistRequest();
116-
$session->unsBeforeWishlistRequest();
117-
}
118-
119146
$productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
120147
if (!$productId) {
121148
$resultRedirect->setPath('*/');
@@ -153,6 +180,7 @@ public function execute()
153180
if ($referer) {
154181
$session->setBeforeWishlistUrl(null);
155182
} else {
183+
// phpcs:ignore
156184
$referer = $this->_redirect->getRefererUrl();
157185
}
158186

@@ -178,7 +206,12 @@ public function execute()
178206
}
179207

180208
if ($this->getRequest()->isAjax()) {
181-
$url = $this->urlBuilder->getUrl('*', $this->redirect->updatePathParams(['wishlist_id' => $wishlist->getId()]));
209+
$url = $this->urlBuilder->getUrl(
210+
'*',
211+
$this->redirect->updatePathParams(
212+
['wishlist_id' => $wishlist->getId()]
213+
)
214+
);
182215
/** @var Json $resultJson */
183216
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
184217
$resultJson->setData(['backUrl' => $url]);
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Wishlist\Model;
11+
12+
use Magento\Framework\Serialize\SerializerInterface;
13+
use Magento\Framework\App\CacheInterface;
14+
use Magento\Framework\Math\Random;
15+
use Psr\Log\LoggerInterface;
16+
use Throwable;
17+
18+
/**
19+
* Service class to Serialize, Cache & Unserialize the data
20+
*/
21+
class DataSerializer
22+
{
23+
/**
24+
* constant for Cache Key Prefix
25+
*/
26+
private const CACHE_KEY_PREFIX = 'wishlist_';
27+
28+
/**
29+
* constant for Cache Life time
30+
*/
31+
private const CACHE_LIFE_TIME = 604800; //7 Days
32+
33+
/**
34+
* constant for Cache Id length
35+
*/
36+
private const CACHE_ID_LENGTH = 32;
37+
38+
/**
39+
* @var SerializerInterface
40+
*/
41+
private $serializer;
42+
43+
/**
44+
* @var CacheInterface
45+
*/
46+
private $cache;
47+
48+
/**
49+
* @var Random
50+
*/
51+
private $random;
52+
53+
/**
54+
* @var LoggerInterface
55+
*/
56+
private $logger;
57+
58+
/**
59+
* Initialize dependencies.
60+
*
61+
* @param SerializerInterface $serializer
62+
* @param CacheInterface $cache
63+
* @param Random $random
64+
* @param LoggerInterface $logger
65+
*/
66+
public function __construct(
67+
SerializerInterface $serializer,
68+
CacheInterface $cache,
69+
Random $random,
70+
LoggerInterface $logger
71+
) {
72+
$this->serializer = $serializer;
73+
$this->cache = $cache;
74+
$this->random = $random;
75+
$this->logger = $logger;
76+
}
77+
78+
/**
79+
* Create Cache key, Serialize & Cache the provided data
80+
*
81+
* @param array $data
82+
* @return string
83+
*/
84+
public function serialize(array $data): string
85+
{
86+
$token = $this->random->getRandomString(self::CACHE_ID_LENGTH);
87+
$cacheKey = self::CACHE_KEY_PREFIX.$token;
88+
89+
$this->cache->save(
90+
$this->serializer->serialize($data),
91+
$cacheKey,
92+
[],
93+
self::CACHE_LIFE_TIME
94+
);
95+
96+
return $token;
97+
}
98+
99+
/**
100+
* Unserialize data for given Token
101+
*
102+
* @param string $token
103+
* @return array
104+
* @throws Throwable if unpredictable error occurred.
105+
*/
106+
public function unserialize(string $token): array
107+
{
108+
if (strlen($token) !== self::CACHE_ID_LENGTH) {
109+
$this->logger->error("Invalid Token '$token' supplied.");
110+
}
111+
112+
$result = [];
113+
$cacheKey = self::CACHE_KEY_PREFIX.$token;
114+
$json = $this->cache->load($cacheKey);
115+
if ($json) {
116+
$result = $this->serializer->unserialize($json);
117+
}
118+
119+
try {
120+
$this->cache->remove($token);
121+
} catch (Throwable $exception) {
122+
$this->logger->error('Unable to remove cache: '.$exception);
123+
}
124+
125+
return $result;
126+
}
127+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Wishlist\Plugin;
11+
12+
use Magento\Wishlist\Model\DataSerializer;
13+
use Magento\Customer\Api\Data\CustomerInterface;
14+
use Magento\Customer\Model\Session as CustomerSession;
15+
use Magento\Customer\Model\EmailNotificationInterface;
16+
use Magento\Framework\UrlInterface;
17+
18+
/**
19+
* Cache wishlist data & Modify back Url
20+
*
21+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
22+
*/
23+
class SaveWishlistDataAndAddReferenceKeyToBackUrl
24+
{
25+
/**
26+
* @var DataSerializer
27+
*/
28+
private $dataSerializer;
29+
30+
/**
31+
* @var CustomerSession
32+
*/
33+
private $customerSession;
34+
35+
/**
36+
* @var UrlInterface
37+
*/
38+
private $urlBuilder;
39+
40+
/**
41+
* @param DataSerializer $dataSerializer
42+
* @param CustomerSession $customerSession
43+
* @param UrlInterface $urlBuilder
44+
*/
45+
public function __construct(
46+
DataSerializer $dataSerializer,
47+
CustomerSession $customerSession,
48+
UrlInterface $urlBuilder
49+
) {
50+
$this->dataSerializer = $dataSerializer;
51+
$this->customerSession = $customerSession;
52+
$this->urlBuilder = $urlBuilder;
53+
}
54+
55+
/**
56+
* Before sending New Account Email, Cache wishlist data & Modify back Url
57+
*
58+
* @param EmailNotificationInterface $subject
59+
* @param CustomerInterface $customer
60+
* @param string $type
61+
* @param string $backUrl
62+
* @param int $storeId
63+
* @param string $sendemailStoreId
64+
* @return array
65+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
66+
*/
67+
public function beforeNewAccount(
68+
EmailNotificationInterface $subject,
69+
CustomerInterface $customer,
70+
$type = EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED,
71+
$backUrl = '',
72+
$storeId = null,
73+
$sendemailStoreId = null
74+
): array {
75+
if ($this->customerSession->getBeforeWishlistRequest() != null
76+
&& $customer->getConfirmation() != null
77+
) {
78+
$token = $this->dataSerializer->serialize($this->customerSession->getBeforeWishlistRequest());
79+
$backUrl = $this->urlBuilder->getUrl('wishlist/index/add', ['_query' => ['token' => $token]]);
80+
}
81+
82+
return [$customer, $type, $backUrl, $storeId, $sendemailStoreId];
83+
}
84+
}

0 commit comments

Comments
 (0)