@@ -37,6 +37,7 @@ class HookDisplayBackOfficeHeader implements HookInterface
3737{
3838 private $ module ;
3939 private $ context ;
40+ private $ gaScripts = '' ;
4041
4142 public function __construct (Ps_Googleanalytics $ module , Context $ context )
4243 {
@@ -56,34 +57,31 @@ public function run()
5657 $ this ->context ->controller ->addCSS ($ this ->module ->getPathUri () . 'views/css/ganalytics.css ' );
5758 }
5859
59- $ gaScripts = '' ;
60-
6160 // Render base tag using displayHeader hook with backoffice parameter
62- $ gaScripts .= $ this ->module ->hookDisplayHeader (null , true );
61+ $ this -> gaScripts .= $ this ->module ->hookDisplayHeader (null , true );
6362
6463 // Process manual orders instantly, we have their IDs in cookie
65- $ gaScripts .= $ this ->processManualOrders ();
64+ $ this ->processManualOrders ();
6665
6766 // Backload old orders that failed to load normally
68- $ gaScripts .= $ this ->backloadFailedOrders ();
67+ $ this ->processFailedOrders ();
6968
70- return $ gaScripts ;
69+ return $ this -> gaScripts ;
7170 }
7271
7372 /**
7473 * Checks if there are any orders that failed to be sent normally through front office and processes them
7574 */
76- protected function backloadFailedOrders ()
75+ protected function processFailedOrders ()
7776 {
78- $ gaScripts = '' ;
7977 if (empty (Configuration::get ('GA_BACKLOAD_ENABLED ' ))) {
80- return $ gaScripts ;
78+ return ;
8179 }
8280
8381 // Check for value on how long back we will get them
8482 $ backloadDays = (int ) Configuration::get ('GA_BACKLOAD_DAYS ' );
8583 if ($ backloadDays < 1 ) {
86- return $ gaScripts ;
84+ return ;
8785 }
8886
8987 // Get all failed orders (either not present in our table or not sent)
@@ -96,34 +94,29 @@ protected function backloadFailedOrders()
9694
9795 // Process each failed order
9896 foreach ($ failedOrders as $ row ) {
99- $ gaScripts .= $ this ->processOrder ((int ) $ row ['id_order ' ]);
97+ $ this ->processOrder ((int ) $ row ['id_order ' ]);
10098 }
101-
102- return $ gaScripts ;
10399 }
104100
105101 /**
106102 * Checks if there are any manual orders in cookie and processes them
107103 */
108104 protected function processManualOrders ()
109105 {
110- $ gaScripts = '' ;
111106 $ adminOrders = $ this ->context ->cookie ->__get ('ga_admin_order ' );
112107 if (empty ($ adminOrders )) {
113- return $ gaScripts ;
108+ return ;
114109 }
115110
116111 // Separate them by IDs and process one by one
117112 $ adminOrders = explode (', ' , $ adminOrders );
118113 foreach ($ adminOrders as $ idOrder ) {
119- $ gaScripts .= $ this ->processOrder ((int ) $ idOrder );
114+ $ this ->processOrder ((int ) $ idOrder );
120115 }
121116
122117 // Clean up the cookie
123118 $ this ->context ->cookie ->__unset ('ga_admin_order ' );
124- $ this ->context ->cookie ->write ();
125-
126- return $ gaScripts ;
119+ $ this ->context ->cookie ->write ();;
127120 }
128121
129122 /**
@@ -133,11 +126,10 @@ protected function processManualOrders()
133126 */
134127 public function processOrder ($ idOrder )
135128 {
136- $ gaScripts = '' ;
137129 $ order = new Order ((int ) $ idOrder );
138130
139131 if (!Validate::isLoadedObject ($ order ) || $ order ->getCurrentState () == (int ) Configuration::get ('PS_OS_ERROR ' )) {
140- return $ gaScripts ;
132+ return ;
141133 }
142134
143135 // Load up our handlers and repositories
@@ -152,13 +144,16 @@ public function processOrder($idOrder)
152144 }
153145
154146 // If the order was already sent for some reason, don't do anything
155- if ($ ganalyticsRepository ->orderAlreadySent ((int ) $ order ->id )) {
156- return $ gaScripts ;
147+ if ($ ganalyticsRepository ->hasOrderBeenAlreadySent ((int ) $ order ->id )) {
148+ return ;
157149 }
158150
159151 // Prepare transaction data
160152 $ orderData = $ orderWrapper ->wrapOrder ($ order );
161153
154+ // Empty script for the current order
155+ $ gaScripts = '' ;
156+
162157 // Add payment event
163158 $ gaScripts .= $ this ->module ->getTools ()->renderEvent (
164159 'add_payment_info ' ,
@@ -184,6 +179,6 @@ public function processOrder($idOrder)
184179 $ this ->context ->link ->getAdminLink ('AdminGanalyticsAjax ' )
185180 );
186181
187- return $ gaTagHandler ->generate ($ gaScripts );
182+ $ this -> gaScripts .= $ gaTagHandler ->generate ($ gaScripts );
188183 }
189184}
0 commit comments