From 750469d05751184ee837d7679d7657d24dcf0569 Mon Sep 17 00:00:00 2001 From: woutersteen Date: Thu, 8 Jan 2026 14:01:27 +0100 Subject: [PATCH 1/3] 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 307e07d1..d889b7da 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); + } + /** * Return a configuration value * diff --git a/DataLayer/Event/Purchase.php b/DataLayer/Event/Purchase.php index 7cf489a9..fe01e39f 100644 --- a/DataLayer/Event/Purchase.php +++ b/DataLayer/Event/Purchase.php @@ -49,14 +49,22 @@ public function get(): array return []; } + $currencyCode = $this->config->useBaseCurrency() + ? $order->getBaseCurrencyCode() + : $order->getOrderCurrencyCode(); + + $taxAmount = $this->config->useBaseCurrency() + ? (float)$order->getBaseTaxAmount() + : (float)$order->getTaxAmount(); + return [ 'event' => 'purchase', 'ecommerce' => [ 'transaction_id' => $order->getIncrementId(), 'affiliation' => $this->config->getStoreName(), - 'currency' => $order->getOrderCurrencyCode(), + 'currency' => $currencyCode, 'value' => $this->priceFormatter->format($this->orderTotals->getValueTotal($order)), - 'tax' => $this->priceFormatter->format((float)$order->getTaxAmount()), + 'tax' => $this->priceFormatter->format($taxAmount), '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 076a1e5b..6559a257 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 + + diff --git a/etc/config.xml b/etc/config.xml index 5c6d9f0d..a569489e 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -17,6 +17,7 @@ 0 new,payment_review,pending_payment,holded,processing,complete + 0 From e2a13a9c62bc219e7cb1b42f0e4c45cbbda78a92 Mon Sep 17 00:00:00 2001 From: woutersteen Date: Thu, 8 Jan 2026 14:05:31 +0100 Subject: [PATCH 2/3] 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 677b03214ca88121ebedf5af2109ed56158c5ee1 Mon Sep 17 00:00:00 2001 From: woutersteen Date: Thu, 8 Jan 2026 14:19:42 +0100 Subject: [PATCH 3/3] 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; /**