6
6
namespace Magento \Wishlist \Controller \Index ;
7
7
8
8
use Magento \Catalog \Api \ProductRepositoryInterface ;
9
- use Magento \Framework \App \Action ;
9
+ use Magento \Customer \Model \Session ;
10
+ use Magento \Framework \App \Action \Context ;
10
11
use Magento \Framework \App \Action \HttpPostActionInterface ;
12
+ use Magento \Framework \Controller \Result \Redirect ;
11
13
use Magento \Framework \Data \Form \FormKey \Validator ;
14
+ use Magento \Framework \Exception \LocalizedException ;
12
15
use Magento \Framework \Exception \NotFoundException ;
13
16
use Magento \Framework \Exception \NoSuchEntityException ;
14
17
use Magento \Framework \Controller \ResultFactory ;
18
+ use Magento \Framework \App \ObjectManager ;
19
+ use Magento \Framework \UrlInterface ;
20
+ use Magento \Framework \App \Response \RedirectInterface ;
21
+ use Magento \Framework \Controller \ResultInterface ;
22
+ use Magento \Wishlist \Controller \WishlistProviderInterface ;
15
23
16
24
/**
17
25
* Wish list Add controller
21
29
class Add extends \Magento \Wishlist \Controller \AbstractIndex implements HttpPostActionInterface
22
30
{
23
31
/**
24
- * @var \Magento\Wishlist\Controller\ WishlistProviderInterface
32
+ * @var WishlistProviderInterface
25
33
*/
26
34
protected $ wishlistProvider ;
27
35
28
36
/**
29
- * @var \Magento\Customer\Model\ Session
37
+ * @var Session
30
38
*/
31
39
protected $ _customerSession ;
32
40
@@ -41,38 +49,54 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPost
41
49
protected $ formKeyValidator ;
42
50
43
51
/**
44
- * @param Action\Context $context
45
- * @param \Magento\Customer\Model\Session $customerSession
46
- * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
52
+ * @var RedirectInterface
53
+ */
54
+ private $ redirect ;
55
+
56
+ /**
57
+ * @var UrlInterface
58
+ */
59
+ private $ urlBuilder ;
60
+
61
+ /**
62
+ * @param Context $context
63
+ * @param Session $customerSession
64
+ * @param WishlistProviderInterface $wishlistProvider
47
65
* @param ProductRepositoryInterface $productRepository
48
66
* @param Validator $formKeyValidator
67
+ * @param RedirectInterface|null $redirect
68
+ * @param UrlInterface|null $urlBuilder
49
69
*/
50
70
public function __construct (
51
- Action \ Context $ context ,
52
- \ Magento \ Customer \ Model \ Session $ customerSession ,
53
- \ Magento \ Wishlist \ Controller \ WishlistProviderInterface $ wishlistProvider ,
71
+ Context $ context ,
72
+ Session $ customerSession ,
73
+ WishlistProviderInterface $ wishlistProvider ,
54
74
ProductRepositoryInterface $ productRepository ,
55
- Validator $ formKeyValidator
75
+ Validator $ formKeyValidator ,
76
+ RedirectInterface $ redirect = null ,
77
+ UrlInterface $ urlBuilder = null
56
78
) {
57
79
$ this ->_customerSession = $ customerSession ;
58
80
$ this ->wishlistProvider = $ wishlistProvider ;
59
81
$ this ->productRepository = $ productRepository ;
60
82
$ this ->formKeyValidator = $ formKeyValidator ;
83
+ $ this ->redirect = $ redirect ?: ObjectManager::getInstance ()->get (RedirectInterface::class);
84
+ $ this ->urlBuilder = $ urlBuilder ?: ObjectManager::getInstance ()->get (UrlInterface::class);
61
85
parent ::__construct ($ context );
62
86
}
63
87
64
88
/**
65
89
* Adding new item
66
90
*
67
- * @return \Magento\Framework\Controller\Result\Redirect
91
+ * @return ResultInterface
68
92
* @throws NotFoundException
69
93
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
70
94
* @SuppressWarnings(PHPMD.NPathComplexity)
71
95
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
72
96
*/
73
97
public function execute ()
74
98
{
75
- /** @var \Magento\Framework\Controller\Result\ Redirect $resultRedirect */
99
+ /** @var Redirect $resultRedirect */
76
100
$ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
77
101
if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
78
102
return $ resultRedirect ->setPath ('*/ ' );
@@ -115,7 +139,7 @@ public function execute()
115
139
116
140
$ result = $ wishlist ->addNewItem ($ product , $ buyRequest );
117
141
if (is_string ($ result )) {
118
- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ($ result ));
142
+ throw new LocalizedException (__ ($ result ));
119
143
}
120
144
if ($ wishlist ->isObjectNew ()) {
121
145
$ wishlist ->save ();
@@ -142,7 +166,7 @@ public function execute()
142
166
]
143
167
);
144
168
// phpcs:disable Magento2.Exceptions.ThrowCatch
145
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
169
+ } catch (LocalizedException $ e ) {
146
170
$ this ->messageManager ->addErrorMessage (
147
171
__ ('We can \'t add the item to Wish List right now: %1. ' , $ e ->getMessage ())
148
172
);
@@ -153,7 +177,16 @@ public function execute()
153
177
);
154
178
}
155
179
180
+ if ($ this ->getRequest ()->isAjax ()) {
181
+ $ url = $ this ->urlBuilder ->getUrl ('* ' , $ this ->redirect ->updatePathParams (['wishlist_id ' => $ wishlist ->getId ()]));
182
+ /** @var Json $resultJson */
183
+ $ resultJson = $ this ->resultFactory ->create (ResultFactory::TYPE_JSON );
184
+ $ resultJson ->setData (['backUrl ' => $ url ]);
185
+
186
+ return $ resultJson ;
187
+ }
156
188
$ resultRedirect ->setPath ('* ' , ['wishlist_id ' => $ wishlist ->getId ()]);
189
+
157
190
return $ resultRedirect ;
158
191
}
159
192
}
0 commit comments