1+ <?php
2+ /**
3+ * Copyright © Magefan ([email protected] ). All rights reserved. 4+ * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+ */
6+
7+ namespace Magefan \GoogleTagManager \Controller \LastOrder ;
8+
9+ use Magento \Framework \App \Action \Context ;
10+ use Magefan \GoogleTagManager \Model \Config ;
11+ use Magefan \GoogleTagManager \Model \DataLayer \Purchase ;
12+ use Magento \Sales \Api \OrderRepositoryInterface ;
13+ use Magento \Checkout \Model \Session ;
14+ use Magento \Framework \Controller \Result \JsonFactory ;
15+
16+ class DataLayer extends \Magento \Framework \App \Action \Action
17+ {
18+ /**
19+ * @var Config
20+ */
21+ protected $ config ;
22+
23+ /**
24+ * @var Purchase
25+ */
26+ protected $ purchaseDataLayer ;
27+
28+ /**
29+ * @var OrderRepositoryInterface
30+ */
31+ protected $ orderRepository ;
32+
33+ /**
34+ * @var Session
35+ */
36+ protected $ checkoutSession ;
37+
38+ /**
39+ * @var JsonFactory
40+ */
41+ protected $ jsonResultFactory ;
42+
43+ /**
44+ * ConfirmOrder constructor.
45+ * @param Context $context
46+ * @param Config $config
47+ * @param Purchase $purchaseDataLayer
48+ * @param OrderRepositoryInterface $orderRepository
49+ * @param Session $checkoutSession
50+ * @param JsonFactory $jsonResultFactory
51+ */
52+ public function __construct (
53+ Context $ context ,
54+ Config $ config ,
55+ Purchase $ purchaseDataLayer ,
56+ OrderRepositoryInterface $ orderRepository ,
57+ Session $ checkoutSession ,
58+ JsonFactory $ jsonResultFactory
59+ ) {
60+ parent ::__construct ($ context );
61+ $ this ->config = $ config ;
62+ $ this ->purchaseDataLayer = $ purchaseDataLayer ;
63+ $ this ->orderRepository = $ orderRepository ;
64+ $ this ->checkoutSession = $ checkoutSession ;
65+ $ this ->jsonResultFactory = $ jsonResultFactory ;
66+ }
67+
68+ public function execute ()
69+ {
70+ $ result = $ this ->jsonResultFactory ->create ();
71+ if ($ this ->config ->isEnabled ()) {
72+ $ orderId = $ this ->checkoutSession ->getLastOrderId ();
73+ try {
74+ $ order = $ this ->orderRepository ->get ($ orderId );
75+ } catch (\NoSuchElementException $ e ) {
76+ }
77+ if ($ order && $ order ->getEntityId ()) {
78+ $ dataLayer = $ this ->purchaseDataLayer ->get ($ order );
79+ if ($ dataLayer ) {
80+ $ result = $ this ->jsonResultFactory ->create ();
81+ $ result ->setData ($ dataLayer );
82+ return $ result ;
83+ }
84+ }
85+ }
86+ return $ result ;
87+ }
88+ }
0 commit comments