Skip to content

Commit 480b0b8

Browse files
authored
Merge pull request #20 from KozakSerhii/9910-send-missing-orders
origin/9910-send-missing-orders
2 parents 56edf4f + 6711109 commit 480b0b8

File tree

2 files changed

+88
-43
lines changed

2 files changed

+88
-43
lines changed

Model/Transaction/Log.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Model\Transaction;
10+
11+
use Magefan\GoogleTagManager\Model\ResourceModel\Transaction\CollectionFactory as TransactionCollectionFactory;
12+
use Magefan\GoogleTagManager\Model\TransactionFactory;
13+
use Magefan\GoogleTagManager\Model\TransactionRepository;
14+
use Magento\Sales\Model\Order;
15+
use Magento\Framework\Exception\CouldNotSaveException;
16+
use Psr\Log\LoggerInterface;
17+
18+
class Log
19+
{
20+
21+
/**
22+
* @var TransactionCollectionFactory
23+
*/
24+
protected $transactionCollectionFactory;
25+
26+
/**
27+
* @var TransactionFactory
28+
*/
29+
protected $transactionFactory;
30+
31+
/**
32+
* @var TransactionRepository
33+
*/
34+
protected $transactionRepository;
35+
36+
/**
37+
* @var LoggerInterface
38+
*/
39+
protected $logger;
40+
41+
/**
42+
* Log constructor.
43+
* @param TransactionCollectionFactory $transactionCollectionFactory
44+
* @param TransactionFactory $transactionFactory
45+
* @param TransactionRepository $transactionRepository
46+
* @param LoggerInterface $logger
47+
*/
48+
public function __construct(
49+
TransactionCollectionFactory $transactionCollectionFactory,
50+
TransactionFactory $transactionFactory,
51+
TransactionRepository $transactionRepository,
52+
LoggerInterface $logger
53+
) {
54+
$this->transactionCollectionFactory = $transactionCollectionFactory;
55+
$this->transactionFactory = $transactionFactory;
56+
$this->transactionRepository = $transactionRepository;
57+
$this->logger = $logger;
58+
}
59+
60+
/**
61+
* @param Order $order
62+
* @param string $requester
63+
* @return void
64+
*/
65+
public function logTransaction(\Magento\Sales\Api\Data\OrderInterface $order, string $requester)
66+
{
67+
$transactionModel = $this->transactionFactory->create();
68+
69+
$transactionModel->setTransactionId((string)$order->getIncrementId());
70+
$transactionModel->setStoreId((int)$order->getStoreId());
71+
$transactionModel->setRequester($requester);
72+
73+
try {
74+
$this->transactionRepository->save($transactionModel);
75+
} catch (CouldNotSaveException $e) {
76+
$this->logger->log("Magefan_GoogleTagManager error while logging transaction id: " . $order->getIncrementId()
77+
. ' and requester: ' . $requester);
78+
}
79+
}
80+
}

Plugin/Magefan/GoogleTagManager/Api/DataLayer/PurchaseInterface.php

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Sales\Model\Order;
1616
use Magento\Framework\Exception\CouldNotSaveException;
1717
use Psr\Log\LoggerInterface;
18+
use Magefan\GoogleTagManager\Model\Transaction\Log;
1819

1920
class PurchaseInterface
2021
{
@@ -24,36 +25,21 @@ class PurchaseInterface
2425
protected $transactionCollectionFactory;
2526

2627
/**
27-
* @var TransactionFactory
28+
* @var Log
2829
*/
29-
protected $transactionFactory;
30-
31-
/**
32-
* @var TransactionRepository
33-
*/
34-
protected $transactionRepository;
35-
36-
/**
37-
* @var LoggerInterface
38-
*/
39-
protected $logger;
30+
protected $transactionLogger;
4031

4132
/**
33+
* PurchaseInterface constructor.
4234
* @param TransactionCollectionFactory $transactionCollectionFactory
43-
* @param TransactionFactory $transactionFactory
44-
* @param TransactionRepository $transactionRepository
45-
* @param LoggerInterface $logger
35+
* @param Log $transactionLogger
4636
*/
4737
public function __construct(
4838
TransactionCollectionFactory $transactionCollectionFactory,
49-
TransactionFactory $transactionFactory,
50-
TransactionRepository $transactionRepository,
51-
LoggerInterface $logger
39+
Log $transactionLogger
5240
) {
5341
$this->transactionCollectionFactory = $transactionCollectionFactory;
54-
$this->transactionFactory = $transactionFactory;
55-
$this->transactionRepository = $transactionRepository;
56-
$this->logger = $logger;
42+
$this->transactionLogger = $transactionLogger;
5743
}
5844

5945
/**
@@ -66,7 +52,7 @@ public function __construct(
6652
public function aroundGet(Subject $subject, $proceed, Order $order, string $requester = '')
6753
{
6854
if ($this->isTransactionIdUniqueForRequester($requester, $order)) {
69-
$this->logTransaction($order, $requester);
55+
$this->transactionLogger->logTransaction($order, $requester);
7056
return $proceed($order, $requester);
7157
} else {
7258
return [];
@@ -97,25 +83,4 @@ protected function isTransactionIdUniqueForRequester(string $requester, Order $o
9783

9884
return true;
9985
}
100-
101-
/**
102-
* @param Order $order
103-
* @param string $requester
104-
* @return void
105-
*/
106-
protected function logTransaction(Order $order, string $requester)
107-
{
108-
$transactionModel = $this->transactionFactory->create();
109-
110-
$transactionModel->setTransactionId((string)$order->getIncrementId());
111-
$transactionModel->setStoreId((int)$order->getStoreId());
112-
$transactionModel->setRequester($requester);
113-
114-
try {
115-
$this->transactionRepository->save($transactionModel);
116-
} catch (CouldNotSaveException $e) {
117-
$this->logger->log("Magefan_GoogleTagManager error while logging transaction id: " . $order->getIncrementId()
118-
. ' and requester: ' . $requester);
119-
}
120-
}
12186
}

0 commit comments

Comments
 (0)