diff --git a/app/code/Magento/GoogleAdwords/Helper/Data.php b/app/code/Magento/GoogleAdwords/Helper/Data.php index 3a70cc77cb7d1..e4999ce7372b4 100644 --- a/app/code/Magento/GoogleAdwords/Helper/Data.php +++ b/app/code/Magento/GoogleAdwords/Helper/Data.php @@ -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 */ @@ -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'; + /**#@-*/ /**#@+ @@ -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; + } } diff --git a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php index 904827d9d5bc7..d513fa3b3ad1e 100644 --- a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php +++ b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php @@ -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 diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php index faa28603af62e..e45ff12a9e206 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php @@ -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'], ]; } @@ -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; @@ -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 */ diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php index ccda04d052fc9..4e8d8e34fbea7 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php @@ -5,6 +5,8 @@ */ namespace Magento\GoogleAdwords\Test\Unit\Observer; +use Magento\GoogleAdwords\Helper\Data; + class SetConversionValueObserverTest extends \PHPUnit\Framework\TestCase { /** @@ -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() @@ -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() @@ -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)); diff --git a/app/code/Magento/GoogleAdwords/etc/adminhtml/system.xml b/app/code/Magento/GoogleAdwords/etc/adminhtml/system.xml index c45f1ca1c2fe2..c312028cf63be 100644 --- a/app/code/Magento/GoogleAdwords/etc/adminhtml/system.xml +++ b/app/code/Magento/GoogleAdwords/etc/adminhtml/system.xml @@ -61,6 +61,14 @@ 0 + + + Magento\Config\Model\Config\Source\Yesno + + 1 + 1 + + diff --git a/app/code/Magento/GoogleAdwords/etc/config.xml b/app/code/Magento/GoogleAdwords/etc/config.xml index 2c6427513b53b..69a87f9cc9e9d 100644 --- a/app/code/Magento/GoogleAdwords/etc/config.xml +++ b/app/code/Magento/GoogleAdwords/etc/config.xml @@ -15,6 +15,7 @@ FFFFFF 1 0 + 0 ar bg diff --git a/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml b/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml index 9388a3cb6fcfb..7daad72994b00 100644 --- a/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml +++ b/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml @@ -18,6 +18,9 @@ var google_conversion_color = "getHelper()->getConversionColor() ?>"; var google_conversion_label = "getHelper()->getConversionLabel() ?>"; var google_conversion_value = getHelper()->getConversionValue() ?>; + getHelper()->hasSendConversionValueCurrency() && $block->getHelper()->getConversionValueCurrency()): ?> + var google_conversion_currency = "getHelper()->getConversionValueCurrency() ?>"; + /* ]]> */