Skip to content

Commit 345f94a

Browse files
authored
Merge pull request #304 from WouterSteen/adjusted-transaction-value-base-currency
Adjusted transaction value base currency
2 parents 2be1371 + 54851ff commit 345f94a

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

Config/Config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,26 @@ public function getOrderStatesForPurchaseEvent(): array
229229
return explode(',', (string)$this->getModuleConfigValue('order_states_for_purchase_event'));
230230
}
231231

232+
/**
233+
* Check whether to use base currency for purchase data
234+
*
235+
* @return bool
236+
*/
237+
public function useBaseCurrency(): bool
238+
{
239+
return (bool)$this->getModuleConfigValue('use_base_currency', false);
240+
}
241+
242+
/**
243+
* Get the maximum transaction value threshold
244+
*
245+
* @return float
246+
*/
247+
public function getMaxTransactionValue(): float
248+
{
249+
return (float)$this->getModuleConfigValue('max_transaction_value', 0);
250+
}
251+
232252
/**
233253
* Return a configuration value
234254
*

DataLayer/Event/Purchase.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,26 @@ public function get(): array
4949
return [];
5050
}
5151

52+
$currencyCode = $this->config->useBaseCurrency()
53+
? $order->getBaseCurrencyCode()
54+
: $order->getOrderCurrencyCode();
55+
56+
$taxAmount = $this->config->useBaseCurrency()
57+
? (float)$order->getBaseTaxAmount()
58+
: (float)$order->getTaxAmount();
59+
60+
$orderValue = $this->orderTotals->getValueTotal($order);
61+
$valueAdjusted = $this->calculateValueAdjusted($orderValue);
62+
5263
return [
5364
'event' => 'purchase',
5465
'ecommerce' => [
5566
'transaction_id' => $order->getIncrementId(),
5667
'affiliation' => $this->config->getStoreName(),
57-
'currency' => $order->getOrderCurrencyCode(),
58-
'value' => $this->priceFormatter->format($this->orderTotals->getValueTotal($order)),
59-
'tax' => $this->priceFormatter->format((float)$order->getTaxAmount()),
68+
'currency' => $currencyCode,
69+
'tax' => $this->priceFormatter->format($taxAmount),
70+
'value' => $this->priceFormatter->format($orderValue),
71+
'value_adjusted' => $this->priceFormatter->format($valueAdjusted),
6072
'shipping' => $this->priceFormatter->format($this->orderTotals->getShippingTotal($order)),
6173
'coupon' => $order->getCouponCode(),
6274
'payment_method' => $order->getPayment() ? $order->getPayment()->getMethod() : '',
@@ -107,4 +119,21 @@ private function getDefaultOrderStates(): array
107119
Order::STATE_COMPLETE,
108120
];
109121
}
122+
123+
/**
124+
* Calculate the adjusted transaction value based on the configured maximum
125+
*
126+
* @param float $orderValue
127+
* @return float
128+
*/
129+
private function calculateValueAdjusted(float $orderValue): float
130+
{
131+
$maxTransactionValue = $this->config->getMaxTransactionValue();
132+
133+
if ($maxTransactionValue <= 0) {
134+
return $orderValue;
135+
}
136+
137+
return min($orderValue, $maxTransactionValue);
138+
}
110139
}

Util/OrderTotals.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,32 @@
44
namespace Yireo\GoogleTagManager2\Util;
55

66
use Magento\Sales\Api\Data\OrderInterface;
7+
use Yireo\GoogleTagManager2\Config\Config;
78

89
class OrderTotals
910
{
11+
private Config $config;
12+
13+
public function __construct(Config $config)
14+
{
15+
$this->config = $config;
16+
}
17+
1018
public function getValueTotal(OrderInterface $order): float
1119
{
20+
if ($this->config->useBaseCurrency()) {
21+
return (float)$order->getBaseSubtotal() - abs((float)$order->getBaseDiscountAmount());
22+
}
23+
1224
return (float)$order->getSubtotal() - abs((float)$order->getDiscountAmount());
1325
}
1426

1527
public function getShippingTotal(OrderInterface $order): float
1628
{
29+
if ($this->config->useBaseCurrency()) {
30+
return (float)$order->getBaseShippingAmount() - (float)$order->getBaseShippingDiscountAmount();
31+
}
32+
1733
return (float)$order->getShippingAmount() - (float)$order->getShippingDiscountAmount();
1834
}
1935
}

etc/adminhtml/system.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@
135135
<field id="serverside_enabled">1</field>
136136
</depends>
137137
</field>
138+
<field id="use_base_currency" type="select" translate="label" sortOrder="33" showInDefault="1" showInWebsite="1" showInStore="1">
139+
<label>Send Quote and Purchase data in base currency</label>
140+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
141+
<comment>If enabled, purchase data will be sent in base currency. If disabled, purchase data will be sent in store currency.</comment>
142+
<depends>
143+
<field id="enabled">1</field>
144+
</depends>
145+
</field>
146+
<field id="max_transaction_value" translate="label" sortOrder="33" showInDefault="1" showInWebsite="1" showInStore="1">
147+
<label>Maximum Transaction Value</label>
148+
<comment>Maximum transaction value for purchase events. When an order value exceeds this amount, the transaction_value_adjusted field will be capped at this value. Leave empty or 0 to disable.</comment>
149+
<validate>validate-number validate-zero-or-greater</validate>
150+
<depends>
151+
<field id="enabled">1</field>
152+
</depends>
153+
</field>
138154
</group>
139155
</section>
140156
</system>

etc/config.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<serverside_enabled>0</serverside_enabled>
1818
<serverside_gtm_url/>
1919
<order_states_for_purchase_event>new,payment_review,pending_payment,holded,processing,complete</order_states_for_purchase_event>
20+
<use_base_currency>0</use_base_currency>
21+
<max_transaction_value>0</max_transaction_value>
2022
</settings>
2123
</googletagmanager2>
2224
</default>

0 commit comments

Comments
 (0)