Skip to content

Commit

Permalink
MAGETWO-71642: Option to send currency in Google Adwords when using d…
Browse files Browse the repository at this point in the history
…ynamic value #10558
  • Loading branch information
ishakhsuvarov authored Aug 21, 2017
2 parents 6b0f1e9 + db15473 commit 8c43d81
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
36 changes: 36 additions & 0 deletions app/code/Magento/GoogleAdwords/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
*/
const CONVERSION_VALUE_REGISTRY_NAME = 'google_adwords_conversion_value';

/**
* Google AdWords registry name for conversion value currency
*/
const CONVERSION_VALUE_CURRENCY_REGISTRY_NAME = 'google_adwords_conversion_value_currency';

/**
* Default value for conversion value
*/
Expand All @@ -59,6 +64,11 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper

const XML_PATH_CONVERSION_VALUE = 'google/adwords/conversion_value';

/**
* Google Adwords send order conversion value currency when using dynamic value
*/
const XML_PATH_SEND_CURRENCY = 'google/adwords/send_currency';

/**#@-*/

/**#@+
Expand Down Expand Up @@ -264,4 +274,30 @@ public function getConversionValue()
}
return empty($conversionValue) ? self::CONVERSION_VALUE_DEFAULT : $conversionValue;
}

/**
* Get send order currency to Google Adwords
*
* @return boolean
*/
public function hasSendConversionValueCurrency()
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_SEND_CURRENCY,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}

/**
* Get Google AdWords conversion value currency
*
* @return string|false
*/
public function getConversionValueCurrency()
{
if ($this->hasSendConversionValueCurrency()) {
return (string) $this->_registry->registry(self::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@ public function execute(\Magento\Framework\Event\Observer $observer)
return $this;
}
$this->_collection->addFieldToFilter('entity_id', ['in' => $orderIds]);

$conversionValue = 0;
/** @var $order \Magento\Sales\Model\Order */
$conversionCurrency = false;
$sendOrderCurrency = $this->_helper->hasSendConversionValueCurrency();
foreach ($this->_collection as $order) {
$conversionValue += $order->getBaseGrandTotal();
/** @var $order \Magento\Sales\Api\Data\OrderInterface */
$conversionValue += $sendOrderCurrency ? $order->getGrandTotal() : $order->getBaseGrandTotal();
$conversionCurrency = $sendOrderCurrency ? $order->getOrderCurrencyCode() : false;
}
$this->_registry->register(
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME,
$conversionCurrency
);
$this->_registry->register(
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME,
$conversionValue
Expand Down
26 changes: 25 additions & 1 deletion app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function dataProviderForTestStoreConfig()
['getConversionColor', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_COLOR, 'ffffff'],
['getConversionLabel', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_LABEL, 'Label'],
['getConversionValueType', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE_TYPE, '1'],
['getConversionValueConstant', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE, '0']
['getConversionValueConstant', \Magento\GoogleAdwords\Helper\Data::XML_PATH_CONVERSION_VALUE, '0'],
];
}

Expand All @@ -196,6 +196,13 @@ public function testGetStoreConfigValue($method, $xmlPath, $returnValue)
$this->assertEquals($returnValue, $this->_helper->{$method}());
}

public function testHasSendConversionValueCurrency()
{
$this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);

$this->assertTrue($this->_helper->hasSendConversionValueCurrency());
}

public function testGetConversionValueDynamic()
{
$returnValue = 4.1;
Expand All @@ -221,6 +228,23 @@ public function testGetConversionValueDynamic()
$this->assertEquals($returnValue, $this->_helper->getConversionValue());
}

public function testGetConversionValueCurrency()
{
$returnValueCurrency = 'USD';
$this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true);
$this->_registryMock->expects(
$this->once()
)->method(
'registry'
)->with(
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME
)->will(
$this->returnValue($returnValueCurrency)
);

$this->assertEquals($returnValueCurrency, $this->_helper->getConversionValueCurrency());
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\GoogleAdwords\Test\Unit\Observer;

use Magento\GoogleAdwords\Helper\Data;

class SetConversionValueObserverTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -122,8 +124,11 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds()
{
$ordersIds = [1, 2, 3];
$conversionValue = 0;
$conversionCurrency = 'USD';
$this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true));
$this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true));
$this->_helperMock->expects($this->once())->method('hasSendConversionValueCurrency')
->will($this->returnValue(true));
$this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds));
$this->_eventObserverMock->expects(
$this->once()
Expand All @@ -133,7 +138,10 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds()
$this->returnValue($this->_eventMock)
);

$iteratorMock = $this->createMock(\Iterator::class);
$orderMock = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
$orderMock->expects($this->once())->method('getOrderCurrencyCode')->willReturn($conversionCurrency);

$iteratorMock = new \ArrayIterator([$orderMock]);
$this->_collectionMock->expects($this->any())->method('getIterator')->will($this->returnValue($iteratorMock));
$this->_collectionMock->expects(
$this->once()
Expand All @@ -144,12 +152,18 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds()
['in' => $ordersIds]
);
$this->_registryMock->expects(
$this->once()
$this->atLeastOnce()
)->method(
'register'
)->with(
\Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME,
$conversionValue
)->withConsecutive(
[
Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME,
$conversionCurrency
],
[
Data::CONVERSION_VALUE_REGISTRY_NAME,
$conversionValue,
]
);

$this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock));
Expand Down
8 changes: 8 additions & 0 deletions app/code/Magento/GoogleAdwords/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
<field id="*/*/conversion_value_type">0</field>
</depends>
</field>
<field id="send_currency" translate="label" type="select" sortOrder="18" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Send Order Currency</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="*/*/active">1</field>
<field id="*/*/conversion_value_type">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/GoogleAdwords/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<conversion_color>FFFFFF</conversion_color>
<conversion_value_type>1</conversion_value_type>
<conversion_value>0</conversion_value>
<send_currency>0</send_currency>
<languages>
<ar>ar</ar>
<bg>bg</bg>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
var google_conversion_color = "<?= /* @escapeNotVerified */ $block->getHelper()->getConversionColor() ?>";
var google_conversion_label = "<?= /* @escapeNotVerified */ $block->getHelper()->getConversionLabel() ?>";
var google_conversion_value = <?= /* @escapeNotVerified */ $block->getHelper()->getConversionValue() ?>;
<?php if($block->getHelper()->hasSendConversionValueCurrency() && $block->getHelper()->getConversionValueCurrency()): ?>
var google_conversion_currency = "<?= /* @escapeNotVerified */ $block->getHelper()->getConversionValueCurrency() ?>";
<?php endif; ?>
/* ]]> */
</script>
<script src="<?= /* @escapeNotVerified */ $block->getHelper()->getConversionJsSrc() ?>"></script>
Expand Down

0 comments on commit 8c43d81

Please sign in to comment.