Skip to content

Commit 39aed21

Browse files
committed
Refacto FO order sending
1 parent 49ba592 commit 39aed21

5 files changed

Lines changed: 93 additions & 87 deletions

File tree

classes/GoogleAnalyticsTools.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,39 @@ public function filter($gaScripts, $filterable)
4242
}
4343

4444
/**
45-
* add order transaction
45+
* Renders purchase event for order
4646
*
47-
* @param array $products
47+
* @param array $orderProducts
4848
* @param array $transaction
49+
* @param string $callbackUrl
4950
*
5051
* @return string|void
5152
*/
52-
public function addTransaction($products, $transaction)
53+
public function renderPurchaseEvent($orderProducts, $orderData, $callbackUrl)
5354
{
54-
if (!is_array($products)) {
55+
if (!is_array($orderProducts)) {
5556
return;
5657
}
5758

5859
$callbackData = [
59-
'orderid' => $transaction['id'],
60-
'customer' => $transaction['customer'],
60+
'orderid' => $orderData['id'],
61+
'customer' => $orderData['customer'],
6162
];
6263

6364
$eventData = [
64-
'transaction_id' => (int) $transaction['id'],
65-
'affiliation' => $transaction['affiliation'],
66-
'value' => (float) $transaction['revenue'],
67-
'tax' => (float) $transaction['tax'],
68-
'shipping' => (float) $transaction['shipping'],
69-
'currency' => $transaction['currency'],
65+
'transaction_id' => (int) $orderData['id'],
66+
'affiliation' => $orderData['affiliation'],
67+
'value' => (float) $orderData['revenue'],
68+
'tax' => (float) $orderData['tax'],
69+
'shipping' => (float) $orderData['shipping'],
70+
'currency' => $orderData['currency'],
7071
'items' => [],
7172
'event_callback' => "function() {
72-
$.get('" . $transaction['url'] . "', " . json_encode($callbackData, JSON_UNESCAPED_UNICODE) . ');
73+
$.get('" . $callbackUrl . "', " . json_encode($callbackData, JSON_UNESCAPED_UNICODE) . ');
7374
}',
7475
];
7576

76-
foreach ($products as $product) {
77+
foreach ($orderProducts as $product) {
7778
$eventData['items'][] = [
7879
'item_id' => (int) $product['id'],
7980
'item_name' => $product['name'],
@@ -82,13 +83,11 @@ public function addTransaction($products, $transaction)
8283
];
8384
}
8485

85-
$js = $this->renderEvent(
86+
return $this->renderEvent(
8687
'purchase',
8788
$eventData,
8889
['event_callback']
8990
);
90-
91-
return $js;
9291
}
9392

9493
/**

classes/Hook/HookDisplayBackOfficeHeader.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,25 @@ public function run()
7979
if ($gaOrderRecords) {
8080
$orderWrapper = new OrderWrapper($this->context);
8181
foreach ($gaOrderRecords as $row) {
82-
$transaction = $orderWrapper->wrapOrder($row['id_order']);
83-
if (!empty($transaction)) {
82+
$orderData = $orderWrapper->wrapOrder($row['id_order']);
83+
if (!empty($orderData)) {
8484
// Mark it as successfully sent
8585
$ganalyticsRepository->markOrderAsSent((int) $row['id_order']);
8686

87-
// Generate transaction event
88-
$callbackData = [
89-
'orderid' => (int) $transaction['id'],
90-
'customer' => (int) $transaction['customer'],
91-
];
92-
93-
$eventData = [
94-
'transaction_id' => (int) $transaction['id'],
95-
'affiliation' => $transaction['affiliation'],
96-
'value' => (float) $transaction['revenue'],
97-
'tax' => (float) $transaction['tax'],
98-
'shipping' => (float) $transaction['shipping'],
99-
'currency' => $transaction['currency'],
100-
'event_callback' => "function() {
101-
$.get('" . $transaction['url'] . "', " . json_encode($callbackData, JSON_UNESCAPED_UNICODE) . ');
102-
}',
103-
];
87+
// Add payment data
10488
$gaScripts .= $this->module->getTools()->renderEvent(
105-
'purchase',
106-
$eventData,
107-
['event_callback']
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')
108101
);
109102
}
110103
}

classes/Hook/HookDisplayOrderConfirmation.php

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
2727
use PrestaShop\Module\Ps_Googleanalytics\Repository\GanalyticsRepository;
2828
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
29+
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\OrderWrapper;
2930
use Ps_Googleanalytics;
3031
use Shop;
3132
use 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
/**

classes/Repository/GanalyticsRepository.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ public function findGaOrderByOrderId($orderId)
4141
WHERE id_order = ' . (int) $orderId);
4242
}
4343

44+
/**
45+
* Checks if order is already sent to GA
46+
*
47+
* @param int $idOrder
48+
*
49+
* @return bool
50+
*/
51+
public function orderAlreadySent($idOrder)
52+
{
53+
return (bool) Db::getInstance()->getValue(
54+
'SELECT `sent`
55+
FROM `' . _DB_PREFIX_ . self::TABLE_NAME . '`
56+
WHERE id_order = ' . (int) $idOrder);
57+
}
58+
4459
/**
4560
* findAllByShopIdAndDateAdd
4661
*

classes/Wrapper/OrderWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function wrapOrder($id_order)
5757
'revenue' => (float) $order->total_paid,
5858
'shipping' => (float) $order->total_shipping,
5959
'tax' => (float) $order->total_paid_tax_incl - $order->total_paid_tax_excl,
60-
'url' => $this->context->link->getAdminLink('AdminGanalyticsAjax'),
6160
'customer' => (int) $order->id_customer,
6261
'currency' => $currency->iso_code,
62+
'payment_type' => (int) $order->payment,
6363
];
6464
}
6565
}

0 commit comments

Comments
 (0)