2626use PrestaShop \Module \Ps_Googleanalytics \Handler \GanalyticsJsHandler ;
2727use PrestaShop \Module \Ps_Googleanalytics \Repository \GanalyticsRepository ;
2828use PrestaShop \Module \Ps_Googleanalytics \Wrapper \ProductWrapper ;
29+ use PrestaShop \Module \Ps_Googleanalytics \Wrapper \OrderWrapper ;
2930use Ps_Googleanalytics ;
3031use Shop ;
3132use Validate ;
@@ -49,59 +50,57 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
4950 */
5051 public function run ()
5152 {
53+ $ gaScripts = '' ;
5254 $ order = $ this ->params ['order ' ];
5355
54- if (Validate::isLoadedObject ($ order ) && $ order ->getCurrentState () != (int ) Configuration::get ('PS_OS_ERROR ' )) {
55- $ ganalyticsRepository = new GanalyticsRepository ();
56- $ gaOrderSent = $ ganalyticsRepository ->findGaOrderByOrderId ((int ) $ order ->id );
57-
58- if (false === $ gaOrderSent ) {
59- // Add order to repository, so we can later mark it as sent
60- $ ganalyticsRepository ->addOrder ((int ) $ order ->id , (int ) $ order ->id_shop );
61-
62- $ cart = new Cart ($ order ->id_cart );
63- $ gaTagHandler = new GanalyticsJsHandler ($ this ->module , $ this ->context );
64- $ productWrapper = new ProductWrapper ($ this ->context );
65-
66- // Prepare currency information
67- $ currency = new Currency ((int ) $ order ->id_currency );
68-
69- // Add payment data
70- $ eventData = [
71- 'currency ' => $ currency ->iso_code ,
72- 'payment_type ' => $ order ->payment ,
73- ];
74- $ gaScripts = $ this ->module ->getTools ()->renderEvent (
75- 'add_payment_info ' ,
76- $ eventData
77- );
78-
79- // Prepare transaction data
80- $ transaction = [
81- 'id ' => (int ) $ order ->id ,
82- 'affiliation ' => Shop::isFeatureActive () ? $ this ->context ->shop ->name : Configuration::get ('PS_SHOP_NAME ' ),
83- 'revenue ' => (float ) $ order ->total_paid ,
84- 'shipping ' => (float ) $ order ->total_shipping ,
85- 'tax ' => (float ) $ order ->total_paid_tax_incl - $ order ->total_paid_tax_excl ,
86- 'url ' => $ this ->context ->link ->getModuleLink ('ps_googleanalytics ' , 'ajax ' , [], true ),
87- 'customer ' => (int ) $ order ->id_customer ,
88- 'currency ' => $ currency ->iso_code ,
89- ];
90-
91- // Prepare order products
92- $ orderProducts = [];
93- foreach ($ cart ->getProducts () as $ order_product ) {
94- $ orderProducts [] = $ productWrapper ->wrapProduct ($ order_product );
95- }
96-
97- // Render transaction code
98- $ gaScripts .= $ this ->module ->getTools ()->addTransaction ($ orderProducts , $ transaction );
56+ if (!Validate::isLoadedObject ($ order ) || $ order ->getCurrentState () == (int ) Configuration::get ('PS_OS_ERROR ' )) {
57+ return $ gaScripts ;
58+ }
9959
100- return $ gaTagHandler ->generate ($ gaScripts );
60+ // Load up the order repository and try to find the order
61+ $ ganalyticsRepository = new GanalyticsRepository ();
62+ if ($ ganalyticsRepository ->orderAlreadySent ((int ) $ order ->id )) {
63+ return $ gaScripts ;
64+ }
65+
66+ // Add order to repository, so we can later mark it as sent
67+ // Repository inserts ignore, so no worries
68+ $ ganalyticsRepository ->addOrder ((int ) $ order ->id , (int ) $ order ->id_shop );
69+
70+ // Load up our handlers
71+ $ gaTagHandler = new GanalyticsJsHandler ($ this ->module , $ this ->context );
72+ $ productWrapper = new ProductWrapper ($ this ->context );
73+ $ orderWrapper = new OrderWrapper ($ this ->context );
74+
75+ // Prepare transaction data
76+ $ orderData = $ orderWrapper ->wrapOrder ((int ) $ order ->id );
77+
78+ // Add payment event
79+ $ gaScripts .= $ this ->module ->getTools ()->renderEvent (
80+ 'add_payment_info ' ,
81+ [
82+ 'currency ' => $ orderData ['currency ' ],
83+ 'payment_type ' => $ orderData ['payment_type ' ],
84+ ]
85+ );
86+
87+ // Prepare order products
88+ $ orderProducts = [];
89+ $ cart = new Cart ($ order ->id_cart );
90+ if (Validate::isLoadedObject ($ cart )) {
91+ foreach ($ cart ->getProducts () as $ order_product ) {
92+ $ orderProducts [] = $ productWrapper ->wrapProduct ($ order_product );
10193 }
10294 }
10395
104- return '' ;
96+ // Render transaction code
97+ $ gaScripts .= $ this ->module ->getTools ()->renderPurchaseEvent (
98+ $ orderProducts ,
99+ $ orderData ,
100+ $ this ->context ->link ->getModuleLink ('ps_googleanalytics ' , 'ajax ' , [], true )
101+ );
102+
103+ return $ gaTagHandler ->generate ($ gaScripts );
105104 }
106105
107106 /**
0 commit comments