diff --git a/Config/Config.php b/Config/Config.php
index 307e07d1..5ad93002 100644
--- a/Config/Config.php
+++ b/Config/Config.php
@@ -229,6 +229,26 @@ public function getOrderStatesForPurchaseEvent(): array
return explode(',', (string)$this->getModuleConfigValue('order_states_for_purchase_event'));
}
+ /**
+ * Check whether to use base currency for purchase data
+ *
+ * @return bool
+ */
+ public function useBaseCurrency(): bool
+ {
+ return (bool)$this->getModuleConfigValue('use_base_currency', false);
+ }
+
+ /**
+ * Get the maximum transaction value threshold
+ *
+ * @return float
+ */
+ public function getMaxTransactionValue(): float
+ {
+ return (float)$this->getModuleConfigValue('max_transaction_value', 0);
+ }
+
/**
* Return a configuration value
*
diff --git a/DataLayer/Event/Purchase.php b/DataLayer/Event/Purchase.php
index 7cf489a9..cc63fcfb 100644
--- a/DataLayer/Event/Purchase.php
+++ b/DataLayer/Event/Purchase.php
@@ -49,14 +49,26 @@ public function get(): array
return [];
}
+ $currencyCode = $this->config->useBaseCurrency()
+ ? $order->getBaseCurrencyCode()
+ : $order->getOrderCurrencyCode();
+
+ $taxAmount = $this->config->useBaseCurrency()
+ ? (float)$order->getBaseTaxAmount()
+ : (float)$order->getTaxAmount();
+
+ $orderValue = $this->orderTotals->getValueTotal($order);
+ $valueAdjusted = $this->calculateValueAdjusted($orderValue);
+
return [
'event' => 'purchase',
'ecommerce' => [
'transaction_id' => $order->getIncrementId(),
'affiliation' => $this->config->getStoreName(),
- 'currency' => $order->getOrderCurrencyCode(),
- 'value' => $this->priceFormatter->format($this->orderTotals->getValueTotal($order)),
- 'tax' => $this->priceFormatter->format((float)$order->getTaxAmount()),
+ 'currency' => $currencyCode,
+ 'tax' => $this->priceFormatter->format($taxAmount),
+ 'value' => $this->priceFormatter->format($orderValue),
+ 'value_adjusted' => $this->priceFormatter->format($valueAdjusted),
'shipping' => $this->priceFormatter->format($this->orderTotals->getShippingTotal($order)),
'coupon' => $order->getCouponCode(),
'payment_method' => $order->getPayment() ? $order->getPayment()->getMethod() : '',
@@ -107,4 +119,21 @@ private function getDefaultOrderStates(): array
Order::STATE_COMPLETE,
];
}
+
+ /**
+ * Calculate the adjusted transaction value based on the configured maximum
+ *
+ * @param float $orderValue
+ * @return float
+ */
+ private function calculateValueAdjusted(float $orderValue): float
+ {
+ $maxTransactionValue = $this->config->getMaxTransactionValue();
+
+ if ($maxTransactionValue <= 0) {
+ return $orderValue;
+ }
+
+ return min($orderValue, $maxTransactionValue);
+ }
}
diff --git a/Util/OrderTotals.php b/Util/OrderTotals.php
index 9c60da01..d2257785 100644
--- a/Util/OrderTotals.php
+++ b/Util/OrderTotals.php
@@ -4,16 +4,32 @@
namespace Yireo\GoogleTagManager2\Util;
use Magento\Sales\Api\Data\OrderInterface;
+use Yireo\GoogleTagManager2\Config\Config;
class OrderTotals
{
+ private Config $config;
+
+ public function __construct(Config $config)
+ {
+ $this->config = $config;
+ }
+
public function getValueTotal(OrderInterface $order): float
{
+ if ($this->config->useBaseCurrency()) {
+ return (float)$order->getBaseSubtotal() - abs((float)$order->getBaseDiscountAmount());
+ }
+
return (float)$order->getSubtotal() - abs((float)$order->getDiscountAmount());
}
public function getShippingTotal(OrderInterface $order): float
{
+ if ($this->config->useBaseCurrency()) {
+ return (float)$order->getBaseShippingAmount() - (float)$order->getBaseShippingDiscountAmount();
+ }
+
return (float)$order->getShippingAmount() - (float)$order->getShippingDiscountAmount();
}
}
diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml
index 076a1e5b..57958490 100644
--- a/etc/adminhtml/system.xml
+++ b/etc/adminhtml/system.xml
@@ -135,6 +135,22 @@
1
+
+
+ Magento\Config\Model\Config\Source\Yesno
+ If enabled, purchase data will be sent in base currency. If disabled, purchase data will be sent in store currency.
+
+ 1
+
+
+
+
+ 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.
+ validate-number validate-zero-or-greater
+
+ 1
+
+
diff --git a/etc/config.xml b/etc/config.xml
index 5c6d9f0d..853662cc 100644
--- a/etc/config.xml
+++ b/etc/config.xml
@@ -17,6 +17,8 @@
0
new,payment_review,pending_payment,holded,processing,complete
+ 0
+ 0