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/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