Skip to content
Merged
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
20 changes: 20 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
35 changes: 32 additions & 3 deletions DataLayer/Event/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() : '',
Expand Down Expand Up @@ -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);
}
}
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();
}
}
16 changes: 16 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@
<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>
<field id="max_transaction_value" translate="label" sortOrder="33" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Maximum Transaction Value</label>
<comment>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.</comment>
<validate>validate-number validate-zero-or-greater</validate>
<depends>
<field id="enabled">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down
2 changes: 2 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<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>
<max_transaction_value>0</max_transaction_value>
</settings>
</googletagmanager2>
</default>
Expand Down
Loading