From 77db871cded3ae59cb1827f494e28456181683cb Mon Sep 17 00:00:00 2001 From: woutersteen Date: Fri, 9 Jan 2026 10:34:14 +0100 Subject: [PATCH 1/4] Added value adjusted field in purchase DL --- Config/Config.php | 10 ++++++++++ DataLayer/Event/Purchase.php | 23 ++++++++++++++++++++++- etc/adminhtml/system.xml | 8 ++++++++ etc/config.xml | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Config/Config.php b/Config/Config.php index 307e07d1..9cfc4780 100644 --- a/Config/Config.php +++ b/Config/Config.php @@ -229,6 +229,16 @@ public function getOrderStatesForPurchaseEvent(): array return explode(',', (string)$this->getModuleConfigValue('order_states_for_purchase_event')); } + /** + * 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..da20c839 100644 --- a/DataLayer/Event/Purchase.php +++ b/DataLayer/Event/Purchase.php @@ -49,13 +49,17 @@ public function get(): array return []; } + $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)), + 'value' => $this->priceFormatter->format($orderValue), + 'value_adjusted' => $this->priceFormatter->format($valueAdjusted), 'tax' => $this->priceFormatter->format((float)$order->getTaxAmount()), 'shipping' => $this->priceFormatter->format($this->orderTotals->getShippingTotal($order)), 'coupon' => $order->getCouponCode(), @@ -107,4 +111,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/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 076a1e5b..45afd32d 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -135,6 +135,14 @@ 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..97c8ac4c 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -17,6 +17,7 @@ 0 new,payment_review,pending_payment,holded,processing,complete + 0 From 04dc06b8bf57282e3a09d99caf44c75593d5a9d4 Mon Sep 17 00:00:00 2001 From: woutersteen Date: Thu, 8 Jan 2026 14:01:27 +0100 Subject: [PATCH 2/4] Created config setting to send Cart & Purchase data in base currency or store currency --- Config/Config.php | 10 ++++++++++ DataLayer/Event/Purchase.php | 12 ++++++++++-- DataLayer/Mapper/OrderItemDataMapper.php | 12 +++++------- Util/OrderTotals.php | 16 ++++++++++++++++ etc/adminhtml/system.xml | 8 ++++++++ etc/config.xml | 1 + 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/Config/Config.php b/Config/Config.php index 9cfc4780..5ad93002 100644 --- a/Config/Config.php +++ b/Config/Config.php @@ -229,6 +229,16 @@ 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 * diff --git a/DataLayer/Event/Purchase.php b/DataLayer/Event/Purchase.php index da20c839..cc63fcfb 100644 --- a/DataLayer/Event/Purchase.php +++ b/DataLayer/Event/Purchase.php @@ -49,6 +49,14 @@ 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); @@ -57,10 +65,10 @@ public function get(): array 'ecommerce' => [ 'transaction_id' => $order->getIncrementId(), 'affiliation' => $this->config->getStoreName(), - 'currency' => $order->getOrderCurrencyCode(), + 'currency' => $currencyCode, + 'tax' => $this->priceFormatter->format($taxAmount), 'value' => $this->priceFormatter->format($orderValue), 'value_adjusted' => $this->priceFormatter->format($valueAdjusted), - 'tax' => $this->priceFormatter->format((float)$order->getTaxAmount()), 'shipping' => $this->priceFormatter->format($this->orderTotals->getShippingTotal($order)), 'coupon' => $order->getCouponCode(), 'payment_method' => $order->getPayment() ? $order->getPayment()->getMethod() : '', diff --git a/DataLayer/Mapper/OrderItemDataMapper.php b/DataLayer/Mapper/OrderItemDataMapper.php index 632f133b..070b9914 100644 --- a/DataLayer/Mapper/OrderItemDataMapper.php +++ b/DataLayer/Mapper/OrderItemDataMapper.php @@ -2,7 +2,6 @@ namespace Yireo\GoogleTagManager2\DataLayer\Mapper; -use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Config\ScopeConfigInterface; @@ -23,13 +22,12 @@ class OrderItemDataMapper private ScopeConfigInterface $scopeConfig; private array $dataLayerMapping; - /** - * @param ProductDataMapper $productDataMapper + * @param ProductDataMapper $productDataMapper * @param ProductRepositoryInterface $productRepository - * @param PriceFormatter $priceFormatter - * @param ScopeConfigInterface $scopeConfig - * @param array $dataLayerMapping + * @param PriceFormatter $priceFormatter + * @param ScopeConfigInterface $scopeConfig + * @param array $dataLayerMapping */ public function __construct( ProductDataMapper $productDataMapper, @@ -109,7 +107,7 @@ private function getPrice(OrderItemInterface $orderItem): float /** * @param OrderItemInterface $orderItem - * @param array $data + * @param array $data * * @return array */ 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 45afd32d..57958490 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -135,6 +135,14 @@ 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. diff --git a/etc/config.xml b/etc/config.xml index 97c8ac4c..853662cc 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -17,6 +17,7 @@ 0 new,payment_review,pending_payment,holded,processing,complete + 0 0 From 015ff4625a8d50e8d098a2b48b52498def6a43c0 Mon Sep 17 00:00:00 2001 From: woutersteen Date: Thu, 8 Jan 2026 14:05:31 +0100 Subject: [PATCH 3/4] Reset OrderItemDataMapper --- DataLayer/Mapper/OrderItemDataMapper.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/DataLayer/Mapper/OrderItemDataMapper.php b/DataLayer/Mapper/OrderItemDataMapper.php index 070b9914..9f900b91 100644 --- a/DataLayer/Mapper/OrderItemDataMapper.php +++ b/DataLayer/Mapper/OrderItemDataMapper.php @@ -2,6 +2,7 @@ namespace Yireo\GoogleTagManager2\DataLayer\Mapper; +use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\Framework\App\Config\ScopeConfigInterface; @@ -20,14 +21,15 @@ class OrderItemDataMapper private ProductRepositoryInterface $productRepository; private PriceFormatter $priceFormatter; private ScopeConfigInterface $scopeConfig; - private array $dataLayerMapping; + private array $dataLayerMapping;gst + /** - * @param ProductDataMapper $productDataMapper + * @param ProductDataMapper $productDataMapper * @param ProductRepositoryInterface $productRepository - * @param PriceFormatter $priceFormatter - * @param ScopeConfigInterface $scopeConfig - * @param array $dataLayerMapping + * @param PriceFormatter $priceFormatter + * @param ScopeConfigInterface $scopeConfig + * @param array $dataLayerMapping */ public function __construct( ProductDataMapper $productDataMapper, @@ -107,7 +109,7 @@ private function getPrice(OrderItemInterface $orderItem): float /** * @param OrderItemInterface $orderItem - * @param array $data + * @param array $data * * @return array */ From 54851ff7201ff7ba508fe914934e92a893b917a3 Mon Sep 17 00:00:00 2001 From: woutersteen Date: Thu, 8 Jan 2026 14:19:42 +0100 Subject: [PATCH 4/4] Reset OrderItemDataMapper --- DataLayer/Mapper/OrderItemDataMapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataLayer/Mapper/OrderItemDataMapper.php b/DataLayer/Mapper/OrderItemDataMapper.php index 9f900b91..632f133b 100644 --- a/DataLayer/Mapper/OrderItemDataMapper.php +++ b/DataLayer/Mapper/OrderItemDataMapper.php @@ -21,7 +21,7 @@ class OrderItemDataMapper private ProductRepositoryInterface $productRepository; private PriceFormatter $priceFormatter; private ScopeConfigInterface $scopeConfig; - private array $dataLayerMapping;gst + private array $dataLayerMapping; /**