1
+ <?php
2
+ /**
3
+ * Landofcoder
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Landofcoder.com license that is
8
+ * available through the world-wide-web at this URL:
9
+ * https://landofcoder.com/terms
10
+ *
11
+ * DISCLAIMER
12
+ *
13
+ * Do not edit or add to this file if you wish to upgrade this extension to newer
14
+ * version in the future.
15
+ *
16
+ * @category Landofcoder
17
+ * @package Lof_Mautic
18
+ * @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19
+ * @license https://landofcoder.com/terms
20
+ */
21
+ namespace Lof \Mautic \Controller \Cart ;
22
+
23
+ use Magento \Framework \App \Action \Action ;
24
+ use Magento \Framework \Controller \ResultFactory ;
25
+ use Magento \Framework \View \Result \PageFactory ;
26
+ use Magento \Framework \App \Action \Context ;
27
+ use Lof \Mautic \Queue \MessageQueues \Order \Publisher ;
28
+ use Lof \Mautic \Model \Mautic \Contact ;
29
+ use Lof \Mautic \Helper \Data ;
30
+
31
+ class Loadquote extends \Magento \Framework \App \Action \Action
32
+ {
33
+ /**
34
+ * @var Publisher|null
35
+ */
36
+ private $ _publisher = null ;
37
+
38
+ /**
39
+ * @var Lof\Mautic\Model\Mautic\Contact|null
40
+ */
41
+ protected $ _mauticContact = null ;
42
+
43
+ /**
44
+ * @var PageFactory
45
+ */
46
+ protected $ pageFactory ;
47
+ /**
48
+ * @var \Magento\Quote\Model\QuoteFactory
49
+ */
50
+ protected $ _quote ;
51
+ /**
52
+ * @var \Magento\Customer\Model\Session
53
+ */
54
+ protected $ _customerSession ;
55
+ /**
56
+ * @var \Ebizmarts\MailChimp\Helper\Data
57
+ */
58
+ protected $ _helper ;
59
+ /**
60
+ * @var \Magento\Framework\Url
61
+ */
62
+ protected $ _urlHelper ;
63
+ /**
64
+ * @var \Magento\Framework\Message\ManagerInterface
65
+ */
66
+ protected $ _message ;
67
+ /**
68
+ * @var \Magento\Customer\Model\Url
69
+ */
70
+ protected $ _customerUrl ;
71
+ /**
72
+ * @var \Magento\Checkout\Model\Session
73
+ */
74
+ protected $ _checkoutSession ;
75
+
76
+ /**
77
+ * Loadquote constructor.
78
+ * @param Context $context
79
+ * @param PageFactory $pageFactory
80
+ * @param \Magento\Quote\Model\QuoteFactory $quote
81
+ * @param \Magento\Customer\Model\Session $customerSession
82
+ * @param \Magento\Checkout\Model\Session $checkoutSession
83
+ * @param \Lof\Mautic\Helper\Data $helper
84
+ * @param \Magento\Framework\Url $urlHelper
85
+ * @param \Magento\Customer\Model\Url $customerUrl
86
+ * @param Publisher $publisher
87
+ * @param Contact $mauticContact
88
+ */
89
+ public function __construct (
90
+ Context $ context ,
91
+ PageFactory $ pageFactory ,
92
+ \Magento \Quote \Model \QuoteFactory $ quote ,
93
+ \Magento \Customer \Model \Session $ customerSession ,
94
+ \Magento \Checkout \Model \Session $ checkoutSession ,
95
+ \Lof \Mautic \Helper \Data $ helper ,
96
+ \Magento \Framework \Url $ urlHelper ,
97
+ \Magento \Customer \Model \Url $ customerUrl ,
98
+ Publisher $ publisher ,
99
+ Contact $ mauticContact
100
+ ) {
101
+
102
+ $ this ->pageFactory = $ pageFactory ;
103
+ $ this ->_quote = $ quote ;
104
+ $ this ->_customerSession = $ customerSession ;
105
+ $ this ->_helper = $ helper ;
106
+ $ this ->_urlHelper = $ urlHelper ;
107
+ $ this ->_message = $ context ->getMessageManager ();
108
+ $ this ->_customerUrl = $ customerUrl ;
109
+ $ this ->_checkoutSession = $ checkoutSession ;
110
+ $ this ->_publisher = $ publisher ;
111
+ $ this ->_mauticContact = $ mauticContact ;
112
+
113
+ parent ::__construct ($ context );
114
+ }
115
+
116
+ public function execute ()
117
+ {
118
+ /** @var \Magento\Framework\View\Result\Page $resultPage */
119
+ $ resultPage = $ this ->pageFactory ->create ();
120
+ $ params = $ this ->getRequest ()->getParams ();
121
+ if (isset ($ params ['id ' ]) && $ params ['id ' ]) {
122
+ $ quote = $ this ->_quote ->create ();
123
+ $ quote ->getResource ()->load ($ quote , $ params ['id ' ]);
124
+ $ magentoStoreId = $ quote ->getStoreId ();
125
+ $ configSecretKey = $ this ->_helper ->getConfig (\Lof \Mautic \Helper \Data::MODULE_ABANDONEDCART_TOKEN );
126
+
127
+ if (!isset ($ params ['token ' ]) || $ params ['token ' ] != $ configSecretKey ) {
128
+ // @error
129
+ $ this ->_message ->addErrorMessage (__ ("You can't access this cart " ));
130
+ $ url = $ this ->_urlHelper ->getUrl (
131
+ $ this ->_helper ->getConfig (
132
+ \Lof \Mautic \Helper \Data::MODULE_ABANDONEDCART_PAGE ,
133
+ $ magentoStoreId
134
+ )
135
+ );
136
+ $ this ->_redirect ($ url );
137
+ } else {
138
+ if (isset ($ params ['mt_cid ' ])) {
139
+ $ url = $ this ->_urlHelper ->getUrl (
140
+ $ this ->_helper ->getConfig (
141
+ \Lof \Mautic \Helper \Data::MODULE_ABANDONEDCART_PAGE ,
142
+ $ magentoStoreId
143
+ ),
144
+ ['mt_cid ' => $ params ['mt_cid ' ]]
145
+ );
146
+ $ quote ->setData ('mautic_campaign_id ' , $ params ['mt_cid ' ]);
147
+ } else {
148
+ $ url = $ this ->_urlHelper ->getUrl (
149
+ $ this ->_helper ->getConfig (
150
+ \Lof \Mautic \Helper \Data::MODULE_ABANDONEDCART_PAGE ,
151
+ $ magentoStoreId
152
+ )
153
+ );
154
+ }
155
+
156
+ $ quote ->getResource ()->save ($ quote );
157
+
158
+ if ($ emailAddress = $ quote ->getCustomerEmail ()) {
159
+ $ this ->processUpdateContactTag ($ emailAddress , $ quote ->getStoreId ());
160
+ }
161
+
162
+ if (!$ quote ->getCustomerId ()) {
163
+ $ this ->_checkoutSession ->setQuoteId ($ quote ->getId ());
164
+ $ this ->_redirect ($ url );
165
+ } else {
166
+ if ($ this ->_customerSession ->isLoggedIn ()) {
167
+ $ this ->_redirect ($ url );
168
+ } else {
169
+ $ this ->_message ->addNoticeMessage (__ ("Login to complete your order " ));
170
+ if (isset ($ params ['mt_cid ' ])) {
171
+ $ url = $ this ->_urlHelper ->getUrl (
172
+ $ this ->_customerUrl ->getLoginUrl (),
173
+ ['mt_cid ' =>$ params ['mt_cid ' ]]
174
+ );
175
+ } else {
176
+ $ url = $ this ->_customerUrl ->getLoginUrl ();
177
+ }
178
+ $ this ->_redirect ($ url );
179
+ }
180
+ }
181
+ }
182
+ }
183
+ return $ resultPage ;
184
+ }
185
+
186
+ /**
187
+ * process update contact tags
188
+ *
189
+ * @param string $email
190
+ * @param int|string|null $storeId
191
+ * @return bool
192
+ */
193
+ protected function processUpdateContactTag ($ email , $ storeId = null )
194
+ {
195
+ if ($ this ->_helper ->isEnabled ($ storeId )) {
196
+ $ removeTags = "- " .(Data::ABANDONED_CART_TAGS );
197
+ $ data = [
198
+ "email " => $ email ,
199
+ "tags " => $ removeTags
200
+ ];
201
+ if (!$ this ->_helper ->isAyncApi ($ storeId )) {
202
+ $ this ->_mauticContact ->exportContact ($ data );
203
+ } else {
204
+ $ this ->_publisher ->execute (
205
+ $ this ->_helper ->encodeData ($ data )
206
+ );
207
+ }
208
+ }
209
+ return true ;
210
+ }
211
+ }
0 commit comments