2222
2323use Configuration ;
2424use Context ;
25+ use Currency ;
2526use Order ;
2627use PrestaShop \Module \Ps_Googleanalytics \Handler \GanalyticsJsHandler ;
2728use PrestaShop \Module \Ps_Googleanalytics \Repository \GanalyticsRepository ;
2829use PrestaShop \Module \Ps_Googleanalytics \Wrapper \OrderWrapper ;
30+ use PrestaShop \Module \Ps_Googleanalytics \Wrapper \ProductWrapper ;
2931use Ps_Googleanalytics ;
3032use Tools ;
3133use Validate ;
@@ -48,66 +50,171 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
4850 */
4951 public function run ()
5052 {
51- $ js = '' ;
53+ // Add assets if we are on configuration page
5254 if (strcmp (Tools::getValue ('configure ' ), $ this ->module ->name ) === 0 ) {
5355 $ this ->context ->controller ->addCSS ($ this ->module ->getPathUri () . 'views/css/ganalytics.css ' );
5456 }
5557
56- $ ga_account_id = Configuration:: get ( ' GA_ACCOUNT_ID ' ) ;
58+ $ gaScripts = '' ;
5759
58- if (! empty ( $ ga_account_id ) && $ this -> module -> active ) {
59- $ gaTagHandler = new GanalyticsJsHandler ( $ this ->module , $ this -> context );
60+ // Render base tag using displayHeader hook with backoffice parameter
61+ $ gaScripts .= $ this ->module -> hookDisplayHeader ( null , true );
6062
61- $ this ->context ->smarty ->assign ('GA_ACCOUNT_ID ' , $ ga_account_id );
63+ // Load up our handlers and repositories
64+ $ ganalyticsRepository = new GanalyticsRepository ();
65+ $ gaTagHandler = new GanalyticsJsHandler ($ this ->module , $ this ->context );
66+ $ productWrapper = new ProductWrapper ($ this ->context );
67+ $ orderWrapper = new OrderWrapper ($ this ->context );
6268
63- $ gaScripts = '' ;
64- if ($ this ->context ->controller ->controller_name == 'AdminOrders ' ) {
65- $ ganalyticsRepository = new GanalyticsRepository ();
69+ // Process manual orders instantly, we have their IDs in cookie
70+ $ gaScripts .= $ this ->processManualOrders ();
71+
72+ // TEMP RETURN
73+ return $ gaTagHandler ->generate ($ gaScripts );
6674
67- if (Tools::getValue ('id_order ' )) {
68- $ order = new Order ((int ) Tools::getValue ('id_order ' ));
69- if (Validate::isLoadedObject ($ order ) && strtotime ('+1 day ' , strtotime ($ order ->date_add )) > time ()) {
70- $ gaOrderSent = $ ganalyticsRepository ->findGaOrderByOrderId ((int ) Tools::getValue ('id_order ' ));
71- if ($ gaOrderSent === false ) {
72- // Add order to repository, so we can later mark it as sent
73- $ ganalyticsRepository ->addOrder ((int ) $ order ->id , (int ) $ order ->id_shop );
74- }
75+ return ;
76+ dump ($ this ->context );
77+ dump ($ _POST );
78+ dump (Tools::getValue ('id_order ' ));
79+ dump ($ _GET );
80+ die;
81+
82+ // TEMP RETURN
83+
84+ // Backload all unsuccessfully sent orders into our database
85+ if ($ this ->context ->controller ->controller_name == 'AdminOrders ' ) {
86+ if (Tools::getValue ('id_order ' )) {
87+ $ order = new Order ((int ) Tools::getValue ('id_order ' ));
88+ if (Validate::isLoadedObject ($ order ) && strtotime ('+1 day ' , strtotime ($ order ->date_add )) > time ()) {
89+ $ gaOrderSent = $ ganalyticsRepository ->findGaOrderByOrderId ((int ) Tools::getValue ('id_order ' ));
90+ if ($ gaOrderSent === false ) {
91+ // Add order to repository, so we can later mark it as sent
92+ $ ganalyticsRepository ->addOrder ((int ) $ order ->id , (int ) $ order ->id_shop );
7593 }
76- } else {
77- $ gaOrderRecords = $ ganalyticsRepository ->findAllByShopIdAndDateAdd ((int ) $ this ->context ->shop ->id );
78-
79- if ($ gaOrderRecords ) {
80- $ orderWrapper = new OrderWrapper ($ this ->context );
81- foreach ($ gaOrderRecords as $ row ) {
82- $ orderData = $ orderWrapper ->wrapOrder ($ row ['id_order ' ]);
83- if (!empty ($ orderData )) {
84- // Mark it as successfully sent
85- $ ganalyticsRepository ->markOrderAsSent ((int ) $ row ['id_order ' ]);
86-
87- // Add payment data
88- $ gaScripts .= $ this ->module ->getTools ()->renderEvent (
89- 'add_payment_info ' ,
90- [
91- 'currency ' => $ orderData ['currency ' ],
92- 'payment_type ' => $ orderData ['payment_type ' ],
93- ]
94- );
95-
96- // Render purchase
97- $ gaScripts .= $ this ->module ->getTools ()->renderPurchaseEvent (
98- [],
99- $ orderData ,
100- $ this ->context ->link ->getAdminLink ('AdminGanalyticsAjax ' )
101- );
102- }
94+ }
95+ } else {
96+ $ gaOrderRecords = $ ganalyticsRepository ->findAllByShopIdAndDateAdd ((int ) $ this ->context ->shop ->id );
97+
98+ if ($ gaOrderRecords ) {
99+ foreach ($ gaOrderRecords as $ row ) {
100+ $ orderData = $ orderWrapper ->wrapOrder ($ row ['id_order ' ]);
101+ if (!empty ($ orderData )) {
102+ // Mark it as successfully sent
103+ $ ganalyticsRepository ->markOrderAsSent ((int ) $ row ['id_order ' ]);
104+
105+ // Add payment data
106+ $ gaScripts .= $ this ->module ->getTools ()->renderEvent (
107+ 'add_payment_info ' ,
108+ [
109+ 'currency ' => $ orderData ['currency ' ],
110+ 'payment_type ' => $ orderData ['payment_type ' ],
111+ ]
112+ );
113+
114+ // Render purchase
115+ $ gaScripts .= $ this ->module ->getTools ()->renderPurchaseEvent (
116+ [],
117+ $ orderData ,
118+ $ this ->context ->link ->getAdminLink ('AdminGanalyticsAjax ' )
119+ );
103120 }
104121 }
105122 }
106123 }
124+ }
125+
126+ return $ gaTagHandler ->generate ($ gaScripts );
127+ }
128+
129+ /**
130+ * @param array $params
131+ */
132+ public function setParams ($ params )
133+ {
134+ $ this ->params = $ params ;
135+ }
136+
137+ /**
138+ * Checks if there are any manual orders in cookie and processes them
139+ */
140+ protected function processManualOrders ()
141+ {
142+ $ adminOrders = $ this ->context ->cookie ->__get ('ga_admin_order ' );
143+ if (empty ($ adminOrders )) {
144+ return ;
145+ }
107146
108- return $ js . $ this ->module ->hookDisplayHeader (null , true ) . $ gaTagHandler ->generate ($ gaScripts );
147+ // Separate them by IDs and process one by one
148+ $ adminOrders = explode (", " , $ adminOrders );
149+ $ gaScripts = '' ;
150+ foreach ($ adminOrders as $ idOrder ) {
151+ $ gaScripts .= $ this ->processOrder ((int ) $ idOrder );
109152 }
110153
111- return $ js ;
154+ // Clean up the cookie
155+ $ this ->context ->cookie ->__unset ('ga_admin_order ' );
156+ $ this ->context ->cookie ->write ();
157+
158+ return $ gaScripts ;
159+ }
160+
161+ /**
162+ * Renders tracking code for given order
163+ *
164+ * @param int $idOrder
165+ */
166+ public function processOrder ($ idOrder )
167+ {
168+ $ gaScripts = '' ;
169+ $ order = new Order ((int ) $ idOrder );
170+
171+ if (!Validate::isLoadedObject ($ order ) || $ order ->getCurrentState () == (int ) Configuration::get ('PS_OS_ERROR ' )) {
172+ return $ gaScripts ;
173+ }
174+
175+ // Load up our handlers and repositories
176+ $ ganalyticsRepository = new GanalyticsRepository ();
177+ $ gaTagHandler = new GanalyticsJsHandler ($ this ->module , $ this ->context );
178+ $ productWrapper = new ProductWrapper ($ this ->context );
179+ $ orderWrapper = new OrderWrapper ($ this ->context );
180+
181+ // If the order was already sent for some reason, don't do anything
182+ if ($ ganalyticsRepository ->orderAlreadySent ((int ) $ order ->id )) {
183+ return $ gaScripts ;
184+ }
185+
186+ // Add order to repository, so we can later mark it as sent
187+ // If revisiting this page, repository inserts ignore, so no worries
188+ $ ganalyticsRepository ->addOrder ((int ) $ order ->id , (int ) $ order ->id_shop );
189+
190+ // Prepare transaction data
191+ $ orderData = $ orderWrapper ->wrapOrder ((int ) $ order ->id );
192+
193+ // Add payment event
194+ $ gaScripts .= $ this ->module ->getTools ()->renderEvent (
195+ 'add_payment_info ' ,
196+ [
197+ 'currency ' => $ orderData ['currency ' ],
198+ 'payment_type ' => $ orderData ['payment_type ' ],
199+ ]
200+ );
201+
202+ // Prepare order products, if the cart still exists
203+ $ orderProducts = [];
204+ $ cart = new Cart ($ order ->id_cart );
205+ if (Validate::isLoadedObject ($ cart )) {
206+ foreach ($ cart ->getProducts () as $ order_product ) {
207+ $ orderProducts [] = $ productWrapper ->wrapProduct ($ order_product );
208+ }
209+ }
210+
211+ // Render transaction code
212+ $ gaScripts .= $ this ->module ->getTools ()->renderPurchaseEvent (
213+ $ orderProducts ,
214+ $ orderData ,
215+ $ this ->context ->link ->getAdminLink ('AdminGanalyticsAjax ' )
216+ );
217+
218+ return $ gaTagHandler ->generate ($ gaScripts );
112219 }
113220}
0 commit comments