Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
12 changes: 10 additions & 2 deletions DataLayer/Event/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() : '',
Expand Down
16 changes: 16 additions & 0 deletions Util/OrderTotals.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@
<field id="serverside_enabled">1</field>
</depends>
</field>
<field id="use_base_currency" type="select" translate="label" sortOrder="33" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Send Quote and Purchase data in base currency</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>If enabled, purchase data will be sent in base currency. If disabled, purchase data will be sent in store currency.</comment>
<depends>
<field id="enabled">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<serverside_enabled>0</serverside_enabled>
<serverside_gtm_url/>
<order_states_for_purchase_event>new,payment_review,pending_payment,holded,processing,complete</order_states_for_purchase_event>
<use_base_currency>0</use_base_currency>
</settings>
</googletagmanager2>
</default>
Expand Down
Loading