2020use Magento \Framework \App \Response \RedirectInterface ;
2121use Magento \Framework \Controller \ResultInterface ;
2222use 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 ]);
0 commit comments