6
6
7
7
namespace Magento \Wishlist \Controller \Index ;
8
8
9
+ use Magento \Catalog \Helper \Product ;
9
10
use Magento \Catalog \Model \Product \Exception as ProductException ;
11
+ use Magento \Checkout \Model \Cart as CheckoutCart ;
12
+ use Magento \Checkout \Helper \Cart as CartHelper ;
10
13
use Magento \Framework \App \Action ;
14
+ use Magento \Framework \App \ObjectManager ;
15
+ use Magento \Framework \Controller \Result \Json ;
16
+ use Magento \Framework \Controller \Result \Redirect ;
11
17
use Magento \Framework \Controller \ResultFactory ;
18
+ use Magento \Framework \Controller \ResultInterface ;
19
+ use Magento \Framework \Data \Form \FormKey \Validator ;
20
+ use Magento \Framework \Escaper ;
21
+ use Magento \Framework \Exception \LocalizedException ;
22
+ use Magento \Framework \Stdlib \Cookie \CookieMetadataFactory ;
23
+ use Magento \Framework \Stdlib \Cookie \PublicCookieMetadata ;
24
+ use Magento \Framework \Stdlib \CookieManagerInterface ;
25
+ use Magento \Wishlist \Controller \AbstractIndex ;
26
+ use Magento \Wishlist \Controller \WishlistProviderInterface ;
27
+ use Magento \Wishlist \Helper \Data ;
28
+ use Magento \Wishlist \Model \Item \OptionFactory ;
29
+ use Magento \Wishlist \Model \ItemFactory ;
30
+ use Magento \Wishlist \Model \LocaleQuantityProcessor ;
31
+ use Magento \Wishlist \Model \ResourceModel \Item \Option \Collection ;
12
32
13
33
/**
14
34
* Add wishlist item to shopping cart and remove from wishlist controller.
15
35
*
16
36
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
37
*/
18
- class Cart extends \ Magento \ Wishlist \ Controller \ AbstractIndex implements Action \HttpPostActionInterface
38
+ class Cart extends AbstractIndex implements Action \HttpPostActionInterface
19
39
{
20
40
/**
21
- * @var \Magento\Wishlist\Controller\ WishlistProviderInterface
41
+ * @var WishlistProviderInterface
22
42
*/
23
43
protected $ wishlistProvider ;
24
44
25
45
/**
26
- * @var \Magento\Wishlist\Model\ LocaleQuantityProcessor
46
+ * @var LocaleQuantityProcessor
27
47
*/
28
48
protected $ quantityProcessor ;
29
49
30
50
/**
31
- * @var \Magento\Wishlist\Model\ ItemFactory
51
+ * @var ItemFactory
32
52
*/
33
53
protected $ itemFactory ;
34
54
35
55
/**
36
- * @var \Magento\Checkout\Model\Cart
56
+ * @var CheckoutCart
37
57
*/
38
58
protected $ cart ;
39
59
40
60
/**
41
- * @var \Magento\Checkout\Helper\Cart
61
+ * @var CartHelper
42
62
*/
43
63
protected $ cartHelper ;
44
64
45
65
/**
46
- * @var \Magento\Wishlist\Model\Item\ OptionFactory
66
+ * @var OptionFactory
47
67
*/
48
68
private $ optionFactory ;
49
69
50
70
/**
51
- * @var \Magento\Catalog\Helper\ Product
71
+ * @var Product
52
72
*/
53
73
protected $ productHelper ;
54
74
55
75
/**
56
- * @var \Magento\Framework\ Escaper
76
+ * @var Escaper
57
77
*/
58
78
protected $ escaper ;
59
79
60
80
/**
61
- * @var \Magento\Wishlist\Helper\ Data
81
+ * @var Data
62
82
*/
63
83
protected $ helper ;
64
84
65
85
/**
66
- * @var \Magento\Framework\Data\Form\FormKey\ Validator
86
+ * @var Validator
67
87
*/
68
88
protected $ formKeyValidator ;
69
89
90
+ /**
91
+ * @var CookieManagerInterface
92
+ */
93
+ private $ cookieManager ;
94
+
95
+ /**
96
+ * @var CookieMetadataFactory
97
+ */
98
+ private $ cookieMetadataFactory ;
99
+
70
100
/**
71
101
* @param Action\Context $context
72
- * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
73
- * @param \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
74
- * @param \Magento\Wishlist\Model\ItemFactory $itemFactory
75
- * @param \Magento\Checkout\Model\Cart $cart
76
- * @param \Magento\Wishlist\Model\Item\OptionFactory $optionFactory
77
- * @param \Magento\Catalog\Helper\Product $productHelper
78
- * @param \Magento\Framework\Escaper $escaper
79
- * @param \Magento\Wishlist\Helper\Data $helper
80
- * @param \Magento\Checkout\Helper\Cart $cartHelper
81
- * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
102
+ * @param WishlistProviderInterface $wishlistProvider
103
+ * @param LocaleQuantityProcessor $quantityProcessor
104
+ * @param ItemFactory $itemFactory
105
+ * @param CheckoutCart $cart
106
+ * @param OptionFactory $optionFactory
107
+ * @param Product $productHelper
108
+ * @param Escaper $escaper
109
+ * @param Data $helper
110
+ * @param CartHelper $cartHelper
111
+ * @param Validator $formKeyValidator
112
+ * @param CookieManagerInterface|null $cookieManager
113
+ * @param CookieMetadataFactory|null $cookieMetadataFactory
114
+ *
82
115
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
83
116
*/
84
117
public function __construct (
85
118
Action \Context $ context ,
86
- \Magento \Wishlist \Controller \WishlistProviderInterface $ wishlistProvider ,
87
- \Magento \Wishlist \Model \LocaleQuantityProcessor $ quantityProcessor ,
88
- \Magento \Wishlist \Model \ItemFactory $ itemFactory ,
89
- \Magento \Checkout \Model \Cart $ cart ,
90
- \Magento \Wishlist \Model \Item \OptionFactory $ optionFactory ,
91
- \Magento \Catalog \Helper \Product $ productHelper ,
92
- \Magento \Framework \Escaper $ escaper ,
93
- \Magento \Wishlist \Helper \Data $ helper ,
94
- \Magento \Checkout \Helper \Cart $ cartHelper ,
95
- \Magento \Framework \Data \Form \FormKey \Validator $ formKeyValidator
119
+ WishlistProviderInterface $ wishlistProvider ,
120
+ LocaleQuantityProcessor $ quantityProcessor ,
121
+ ItemFactory $ itemFactory ,
122
+ CheckoutCart $ cart ,
123
+ OptionFactory $ optionFactory ,
124
+ Product $ productHelper ,
125
+ Escaper $ escaper ,
126
+ Data $ helper ,
127
+ CartHelper $ cartHelper ,
128
+ Validator $ formKeyValidator ,
129
+ ?CookieManagerInterface $ cookieManager = null ,
130
+ ?CookieMetadataFactory $ cookieMetadataFactory = null
96
131
) {
97
132
$ this ->wishlistProvider = $ wishlistProvider ;
98
133
$ this ->quantityProcessor = $ quantityProcessor ;
@@ -104,6 +139,9 @@ public function __construct(
104
139
$ this ->helper = $ helper ;
105
140
$ this ->cartHelper = $ cartHelper ;
106
141
$ this ->formKeyValidator = $ formKeyValidator ;
142
+ $ this ->cookieManager = $ cookieManager ?: ObjectManager::getInstance ()->get (CookieManagerInterface::class);
143
+ $ this ->cookieMetadataFactory = $ cookieMetadataFactory ?:
144
+ ObjectManager::getInstance ()->get (CookieMetadataFactory::class);
107
145
parent ::__construct ($ context );
108
146
}
109
147
@@ -113,14 +151,14 @@ public function __construct(
113
151
* If Product has required options - item removed from wishlist and redirect
114
152
* to product view page with message about needed defined required options
115
153
*
116
- * @return \Magento\Framework\Controller\ ResultInterface
154
+ * @return ResultInterface
117
155
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
118
156
* @SuppressWarnings(PHPMD.NPathComplexity)
119
157
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
120
158
*/
121
159
public function execute ()
122
160
{
123
- /** @var \Magento\Framework\Controller\Result\ Redirect $resultRedirect */
161
+ /** @var Redirect $resultRedirect */
124
162
$ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
125
163
if (!$ this ->formKeyValidator ->validate ($ this ->getRequest ())) {
126
164
return $ resultRedirect ->setPath ('*/*/ ' );
@@ -167,7 +205,7 @@ public function execute()
167
205
);
168
206
169
207
try {
170
- /** @var \Magento\Wishlist\Model\ResourceModel\Item\Option\ Collection $options */
208
+ /** @var Collection $options */
171
209
$ options = $ this ->optionFactory ->create ()->getCollection ()->addItemFilter ([$ itemId ]);
172
210
$ item ->setOptions ($ options ->getOptionsByItem ($ itemId ));
173
211
@@ -187,6 +225,27 @@ public function execute()
187
225
$ this ->escaper ->escapeHtml ($ item ->getProduct ()->getName ())
188
226
);
189
227
$ this ->messageManager ->addSuccessMessage ($ message );
228
+
229
+ $ productsToAdd = [
230
+ [
231
+ 'sku ' => $ item ->getProduct ()->getSku (),
232
+ 'name ' => $ item ->getProduct ()->getName (),
233
+ 'price ' => $ item ->getProduct ()->getFinalPrice (),
234
+ 'qty ' => $ item ->getQty (),
235
+ ]
236
+ ];
237
+
238
+ /** @var PublicCookieMetadata $publicCookieMetadata */
239
+ $ publicCookieMetadata = $ this ->cookieMetadataFactory ->createPublicCookieMetadata ()
240
+ ->setDuration (3600 )
241
+ ->setPath ('/ ' )
242
+ ->setHttpOnly (false );
243
+
244
+ $ this ->cookieManager ->setPublicCookie (
245
+ 'add_to_cart ' ,
246
+ \rawurlencode (\json_encode ($ productsToAdd )),
247
+ $ publicCookieMetadata
248
+ );
190
249
}
191
250
192
251
if ($ this ->cartHelper ->getShouldRedirectToCart ()) {
@@ -199,7 +258,7 @@ public function execute()
199
258
}
200
259
} catch (ProductException $ e ) {
201
260
$ this ->messageManager ->addErrorMessage (__ ('This product(s) is out of stock. ' ));
202
- } catch (\ Magento \ Framework \ Exception \ LocalizedException $ e ) {
261
+ } catch (LocalizedException $ e ) {
203
262
$ this ->messageManager ->addNoticeMessage ($ e ->getMessage ());
204
263
$ redirectUrl = $ configureUrl ;
205
264
} catch (\Exception $ e ) {
@@ -209,7 +268,7 @@ public function execute()
209
268
$ this ->helper ->calculate ();
210
269
211
270
if ($ this ->getRequest ()->isAjax ()) {
212
- /** @var \Magento\Framework\Controller\Result\ Json $resultJson */
271
+ /** @var Json $resultJson */
213
272
$ resultJson = $ this ->resultFactory ->create (ResultFactory::TYPE_JSON );
214
273
$ resultJson ->setData (['backUrl ' => $ redirectUrl ]);
215
274
return $ resultJson ;
0 commit comments