Skip to content

Commit 07b88ce

Browse files
committed
Option to send currency in adwords when using dynamic value
1 parent d19e900 commit 07b88ce

File tree

7 files changed

+78
-2
lines changed

7 files changed

+78
-2
lines changed

app/code/Magento/GoogleAdwords/Helper/Data.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
3535
*/
3636
const CONVERSION_VALUE_REGISTRY_NAME = 'google_adwords_conversion_value';
3737

38+
/**
39+
* Google AdWords registry name for conversion value currency
40+
*/
41+
const CONVERSION_VALUE_CURRENCY_REGISTRY_NAME = 'google_adwords_conversion_value_currency';
42+
3843
/**
3944
* Default value for conversion value
4045
*/
@@ -59,6 +64,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
5964

6065
const XML_PATH_CONVERSION_VALUE = 'google/adwords/conversion_value';
6166

67+
const XML_PATH_SEND_CURRENCY = 'google/adwords/send_currency';
68+
6269
/**#@-*/
6370

6471
/**#@+
@@ -264,4 +271,29 @@ public function getConversionValue()
264271
}
265272
return empty($conversionValue) ? self::CONVERSION_VALUE_DEFAULT : $conversionValue;
266273
}
274+
275+
/**
276+
* Get send order currency to Google Adwords
277+
*
278+
* @return boolean
279+
*/
280+
public function getSendCurrency() {
281+
return $this->scopeConfig->isSetFlag(
282+
self::XML_PATH_SEND_CURRENCY,
283+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
284+
);
285+
}
286+
287+
/**
288+
* Get Google AdWords conversion value currency
289+
*
290+
* @return float
291+
*/
292+
public function getConversionValueCurrency()
293+
{
294+
if ($this->getSendCurrency()) {
295+
return (string) $this->_registry->registry(self::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME);
296+
}
297+
return false;
298+
}
267299
}

app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,20 @@ public function execute(\Magento\Framework\Event\Observer $observer)
6060
$this->_collection->addFieldToFilter('entity_id', ['in' => $orderIds]);
6161
$conversionValue = 0;
6262
/** @var $order \Magento\Sales\Model\Order */
63-
foreach ($this->_collection as $order) {
64-
$conversionValue += $order->getBaseGrandTotal();
63+
64+
if($this->_helper->getSendCurrency()) {
65+
foreach ($this->_collection as $order) {
66+
$conversionValue += $order->getGrandTotal();
67+
$conversionCurrency = $order->getOrderCurrencyCode();
68+
}
69+
$this->_registry->register(
70+
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME,
71+
$conversionCurrency
72+
);
73+
} else {
74+
foreach ($this->_collection as $order) {
75+
$conversionValue += $order->getBaseGrandTotal();
76+
}
6577
}
6678
$this->_registry->register(
6779
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME,

app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public function testGetStoreConfigValue($method, $xmlPath, $returnValue)
199199
public function testGetConversionValueDynamic()
200200
{
201201
$returnValue = 4.1;
202+
$returnValueCurrency = 'USD';
202203
$this->_scopeConfigMock->expects(
203204
$this->any()
204205
)->method(
@@ -217,8 +218,18 @@ public function testGetConversionValueDynamic()
217218
)->will(
218219
$this->returnValue($returnValue)
219220
);
221+
$this->_registryMock->expects(
222+
$this->once()
223+
)->method(
224+
'registry'
225+
)->with(
226+
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME
227+
)->will(
228+
$this->returnValue($returnValueCurrency)
229+
);
220230

221231
$this->assertEquals($returnValue, $this->_helper->getConversionValue());
232+
$this->assertEquals($returnValueCurrency, $this->_helper->getConversionValueCurrency());
222233
}
223234

224235
/**

app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds()
122122
{
123123
$ordersIds = [1, 2, 3];
124124
$conversionValue = 0;
125+
$conversionValueCurrency = 'USD';
125126
$this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true));
126127
$this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true));
127128
$this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds));
@@ -151,6 +152,14 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds()
151152
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME,
152153
$conversionValue
153154
);
155+
$this->_registryMock->expects(
156+
$this->once()
157+
)->method(
158+
'register'
159+
)->with(
160+
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME,
161+
$conversionValueCurrency
162+
);
154163

155164
$this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
156165
}

app/code/Magento/GoogleAdwords/etc/adminhtml/system.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
<field id="*/*/conversion_value_type">0</field>
6262
</depends>
6363
</field>
64+
<field id="send_currency" translate="label" type="select" sortOrder="18" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
65+
<label>Send Order Currency</label>
66+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
67+
<depends>
68+
<field id="*/*/active">1</field>
69+
<field id="*/*/conversion_value_type">1</field>
70+
</depends>
71+
</field>
6472
</group>
6573
</section>
6674
</system>

app/code/Magento/GoogleAdwords/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<conversion_color>FFFFFF</conversion_color>
1616
<conversion_value_type>1</conversion_value_type>
1717
<conversion_value>0</conversion_value>
18+
<send_currency>0</send_currency>
1819
<languages>
1920
<ar>ar</ar>
2021
<bg>bg</bg>

app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
var google_conversion_color = "<?= /* @escapeNotVerified */ $block->getHelper()->getConversionColor() ?>";
1919
var google_conversion_label = "<?= /* @escapeNotVerified */ $block->getHelper()->getConversionLabel() ?>";
2020
var google_conversion_value = <?= /* @escapeNotVerified */ $block->getHelper()->getConversionValue() ?>;
21+
<?php if($block->getHelper()->getSendCurrency() && $block->getHelper()->getConversionValueCurrency()): ?>
22+
var google_conversion_currency = "<?= /* @escapeNotVerified */ $block->getHelper()->getConversionValueCurrency() ?>";
23+
<?php endif; ?>
2124
/* ]]> */
2225
</script>
2326
<script src="<?= /* @escapeNotVerified */ $block->getHelper()->getConversionJsSrc() ?>"></script>

0 commit comments

Comments
 (0)