66namespace Magento \Wishlist \Controller \Index ;
77
88use Magento \Catalog \Api \ProductRepositoryInterface ;
9- use Magento \Framework \App \Action ;
9+ use Magento \Customer \Model \Session ;
10+ use Magento \Framework \App \Action \Context ;
1011use Magento \Framework \App \Action \HttpPostActionInterface ;
12+ use Magento \Framework \Controller \Result \Redirect ;
1113use Magento \Framework \Data \Form \FormKey \Validator ;
14+ use Magento \Framework \Exception \LocalizedException ;
1215use Magento \Framework \Exception \NotFoundException ;
1316use Magento \Framework \Exception \NoSuchEntityException ;
1417use 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 ;
1523
1624/**
1725 * Wish list Add controller
2129class Add extends \Magento \Wishlist \Controller \AbstractIndex implements HttpPostActionInterface
2230{
2331 /**
24- * @var \Magento\Wishlist\Controller\ WishlistProviderInterface
32+ * @var WishlistProviderInterface
2533 */
2634 protected $ wishlistProvider ;
2735
2836 /**
29- * @var \Magento\Customer\Model\ Session
37+ * @var Session
3038 */
3139 protected $ _customerSession ;
3240
@@ -41,38 +49,54 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPost
4149 protected $ formKeyValidator ;
4250
4351 /**
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
4765 * @param ProductRepositoryInterface $productRepository
4866 * @param Validator $formKeyValidator
67+ * @param RedirectInterface|null $redirect
68+ * @param UrlInterface|null $urlBuilder
4969 */
5070 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 ,
5474 ProductRepositoryInterface $ productRepository ,
55- Validator $ formKeyValidator
75+ Validator $ formKeyValidator ,
76+ RedirectInterface $ redirect = null ,
77+ UrlInterface $ urlBuilder = null
5678 ) {
5779 $ this ->_customerSession = $ customerSession ;
5880 $ this ->wishlistProvider = $ wishlistProvider ;
5981 $ this ->productRepository = $ productRepository ;
6082 $ this ->formKeyValidator = $ formKeyValidator ;
83+ $ this ->redirect = $ redirect ?: ObjectManager::getInstance ()->get (RedirectInterface::class);
84+ $ this ->urlBuilder = $ urlBuilder ?: ObjectManager::getInstance ()->get (UrlInterface::class);
6185 parent ::__construct ($ context );
6286 }
6387
6488 /**
6589 * Adding new item
6690 *
67- * @return \Magento\Framework\Controller\Result\Redirect
91+ * @return ResultInterface
6892 * @throws NotFoundException
6993 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
7094 * @SuppressWarnings(PHPMD.NPathComplexity)
7195 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
7296 */
7397 public function execute ()
7498 {
75- /** @var \Magento\Framework\Controller\Result\ Redirect $resultRedirect */
99+ /** @var Redirect $resultRedirect */
76100 $ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
77101 if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
78102 return $ resultRedirect ->setPath ('*/ ' );
@@ -115,7 +139,7 @@ public function execute()
115139
116140 $ result = $ wishlist ->addNewItem ($ product , $ buyRequest );
117141 if (is_string ($ result )) {
118- throw new \ Magento \ Framework \ Exception \ LocalizedException (__ ($ result ));
142+ throw new LocalizedException (__ ($ result ));
119143 }
120144 if ($ wishlist ->isObjectNew ()) {
121145 $ wishlist ->save ();
@@ -142,7 +166,7 @@ public function execute()
142166 ]
143167 );
144168 // phpcs:disable Magento2.Exceptions.ThrowCatch
145- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
169+ } catch (LocalizedException $ e ) {
146170 $ this ->messageManager ->addErrorMessage (
147171 __ ('We can \'t add the item to Wish List right now: %1. ' , $ e ->getMessage ())
148172 );
@@ -153,7 +177,16 @@ public function execute()
153177 );
154178 }
155179
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+ }
156188 $ resultRedirect ->setPath ('* ' , ['wishlist_id ' => $ wishlist ->getId ()]);
189+
157190 return $ resultRedirect ;
158191 }
159192}
0 commit comments