From 07b88ce663a19cacff56bc369d08115ea37dddb3 Mon Sep 17 00:00:00 2001 From: DominicWatts Date: Wed, 16 Aug 2017 22:46:49 +0100 Subject: [PATCH 1/7] Option to send currency in adwords when using dynamic value --- .../Magento/GoogleAdwords/Helper/Data.php | 32 +++++++++++++++++++ .../Observer/SetConversionValueObserver.php | 16 ++++++++-- .../Test/Unit/Helper/DataTest.php | 11 +++++++ .../SetConversionValueObserverTest.php | 9 ++++++ .../GoogleAdwords/etc/adminhtml/system.xml | 8 +++++ app/code/Magento/GoogleAdwords/etc/config.xml | 1 + .../view/frontend/templates/code.phtml | 3 ++ 7 files changed, 78 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/GoogleAdwords/Helper/Data.php b/app/code/Magento/GoogleAdwords/Helper/Data.php index 3a70cc77cb7d1..d391bc02f6e39 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,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper const XML_PATH_CONVERSION_VALUE = 'google/adwords/conversion_value'; + const XML_PATH_SEND_CURRENCY = 'google/adwords/send_currency'; + /**#@-*/ /**#@+ @@ -264,4 +271,29 @@ public function getConversionValue() } return empty($conversionValue) ? self::CONVERSION_VALUE_DEFAULT : $conversionValue; } + + /** + * Get send order currency to Google Adwords + * + * @return boolean + */ + public function getSendCurrency() { + return $this->scopeConfig->isSetFlag( + self::XML_PATH_SEND_CURRENCY, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE + ); + } + + /** + * Get Google AdWords conversion value currency + * + * @return float + */ + public function getConversionValueCurrency() + { + if ($this->getSendCurrency()) { + 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..2b6b98002e03b 100644 --- a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php +++ b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php @@ -60,8 +60,20 @@ public function execute(\Magento\Framework\Event\Observer $observer) $this->_collection->addFieldToFilter('entity_id', ['in' => $orderIds]); $conversionValue = 0; /** @var $order \Magento\Sales\Model\Order */ - foreach ($this->_collection as $order) { - $conversionValue += $order->getBaseGrandTotal(); + + if($this->_helper->getSendCurrency()) { + foreach ($this->_collection as $order) { + $conversionValue += $order->getGrandTotal(); + $conversionCurrency = $order->getOrderCurrencyCode(); + } + $this->_registry->register( + \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME, + $conversionCurrency + ); + } else { + foreach ($this->_collection as $order) { + $conversionValue += $order->getBaseGrandTotal(); + } } $this->_registry->register( \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME, diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php index faa28603af62e..22022325e9dcb 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php @@ -199,6 +199,7 @@ public function testGetStoreConfigValue($method, $xmlPath, $returnValue) public function testGetConversionValueDynamic() { $returnValue = 4.1; + $returnValueCurrency = 'USD'; $this->_scopeConfigMock->expects( $this->any() )->method( @@ -217,8 +218,18 @@ public function testGetConversionValueDynamic() )->will( $this->returnValue($returnValue) ); + $this->_registryMock->expects( + $this->once() + )->method( + 'registry' + )->with( + \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME + )->will( + $this->returnValue($returnValueCurrency) + ); $this->assertEquals($returnValue, $this->_helper->getConversionValue()); + $this->assertEquals($returnValueCurrency, $this->_helper->getConversionValueCurrency()); } /** diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php index ccda04d052fc9..dd0a224d444f0 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php @@ -122,6 +122,7 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds() { $ordersIds = [1, 2, 3]; $conversionValue = 0; + $conversionValueCurrency = 'USD'; $this->_helperMock->expects($this->once())->method('isGoogleAdwordsActive')->will($this->returnValue(true)); $this->_helperMock->expects($this->once())->method('isDynamicConversionValue')->will($this->returnValue(true)); $this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds)); @@ -151,6 +152,14 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds() \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_REGISTRY_NAME, $conversionValue ); + $this->_registryMock->expects( + $this->once() + )->method( + 'register' + )->with( + \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME, + $conversionValueCurrency + ); $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..b7cd7e0a93e23 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()->getSendCurrency() && $block->getHelper()->getConversionValueCurrency()): ?> + var google_conversion_currency = "getHelper()->getConversionValueCurrency() ?>"; + /* ]]> */ From fd1b971471a9a9f00ceb1d2207cc2ad2cd87ab22 Mon Sep 17 00:00:00 2001 From: DominicWatts Date: Thu, 17 Aug 2017 22:22:55 +0100 Subject: [PATCH 2/7] changes in response to unit and static tests --- .../Magento/GoogleAdwords/Helper/Data.php | 7 ++++--- .../Observer/SetConversionValueObserver.php | 20 +++++++------------ .../Test/Unit/Helper/DataTest.php | 3 ++- .../SetConversionValueObserverTest.php | 5 +++-- .../view/frontend/templates/code.phtml | 2 +- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/app/code/Magento/GoogleAdwords/Helper/Data.php b/app/code/Magento/GoogleAdwords/Helper/Data.php index d391bc02f6e39..9ef7f6217e03f 100644 --- a/app/code/Magento/GoogleAdwords/Helper/Data.php +++ b/app/code/Magento/GoogleAdwords/Helper/Data.php @@ -277,7 +277,8 @@ public function getConversionValue() * * @return boolean */ - public function getSendCurrency() { + public function hasSendCurrency() + { return $this->scopeConfig->isSetFlag( self::XML_PATH_SEND_CURRENCY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE @@ -291,9 +292,9 @@ public function getSendCurrency() { */ public function getConversionValueCurrency() { - if ($this->getSendCurrency()) { + if ($this->hasSendCurrency()) { 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 2b6b98002e03b..3442fd01369b6 100644 --- a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php +++ b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php @@ -61,20 +61,14 @@ public function execute(\Magento\Framework\Event\Observer $observer) $conversionValue = 0; /** @var $order \Magento\Sales\Model\Order */ - if($this->_helper->getSendCurrency()) { - foreach ($this->_collection as $order) { - $conversionValue += $order->getGrandTotal(); - $conversionCurrency = $order->getOrderCurrencyCode(); - } - $this->_registry->register( - \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME, - $conversionCurrency - ); - } else { - foreach ($this->_collection as $order) { - $conversionValue += $order->getBaseGrandTotal(); - } + foreach ($this->_collection as $order) { + $conversionValue += ($this->_helper->hasSendCurrency()) ? $order->getGrandTotal() : $order->getBaseGrandTotal(); + $conversionCurrency = ($this->_helper->hasSendCurrency()) ? $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 22022325e9dcb..e2cf3dcc48ac8 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php @@ -171,7 +171,8 @@ 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'], + ['hasSendCurrency', \Magento\GoogleAdwords\Helper\Data::XML_PATH_SEND_CURRENCY, '1'] ]; } diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php index dd0a224d444f0..9170cb114cef2 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php @@ -122,9 +122,10 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds() { $ordersIds = [1, 2, 3]; $conversionValue = 0; - $conversionValueCurrency = 'USD'; + $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('hasSendCurrency')->will($this->returnValue(true)); $this->_eventMock->expects($this->once())->method('getOrderIds')->will($this->returnValue($ordersIds)); $this->_eventObserverMock->expects( $this->once() @@ -158,7 +159,7 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds() 'register' )->with( \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME, - $conversionValueCurrency + $conversionCurrency ); $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock)); diff --git a/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml b/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml index b7cd7e0a93e23..0360d9c5f78ca 100644 --- a/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml +++ b/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml @@ -18,7 +18,7 @@ var google_conversion_color = "getHelper()->getConversionColor() ?>"; var google_conversion_label = "getHelper()->getConversionLabel() ?>"; var google_conversion_value = getHelper()->getConversionValue() ?>; - getHelper()->getSendCurrency() && $block->getHelper()->getConversionValueCurrency()): ?> + getHelper()->hasSendCurrency() && $block->getHelper()->getConversionValueCurrency()): ?> var google_conversion_currency = "getHelper()->getConversionValueCurrency() ?>"; /* ]]> */ From cf6dd9b12699e0d9b1312cb67da21c5898f42b44 Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Fri, 18 Aug 2017 12:41:38 +0300 Subject: [PATCH 3/7] magento/magento2#10558: Option to send currency in Google Adwords when using dynamic value - Fixes per failed tests --- .../Observer/SetConversionValueObserver.php | 10 ++++--- .../Test/Unit/Helper/DataTest.php | 20 +++++++++++-- .../SetConversionValueObserverTest.php | 29 ++++++++++--------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php index 3442fd01369b6..51d5d6c5766dd 100644 --- a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php +++ b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php @@ -58,12 +58,14 @@ 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 */ + $conversionValue = 0; + $conversionCurrency = false; + $orderHasSendCurrency = $this->_helper->hasSendCurrency(); foreach ($this->_collection as $order) { - $conversionValue += ($this->_helper->hasSendCurrency()) ? $order->getGrandTotal() : $order->getBaseGrandTotal(); - $conversionCurrency = ($this->_helper->hasSendCurrency()) ? $order->getOrderCurrencyCode() : false; + /** @var $order \Magento\Sales\Model\Order */ + $conversionValue += $orderHasSendCurrency ? $order->getGrandTotal() : $order->getBaseGrandTotal(); + $conversionCurrency = $orderHasSendCurrency ? $order->getOrderCurrencyCode() : false; } $this->_registry->register( \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME, diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php index e2cf3dcc48ac8..500b0d822dc69 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php @@ -172,7 +172,6 @@ public function dataProviderForTestStoreConfig() ['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'], - ['hasSendCurrency', \Magento\GoogleAdwords\Helper\Data::XML_PATH_SEND_CURRENCY, '1'] ]; } @@ -197,10 +196,16 @@ public function testGetStoreConfigValue($method, $xmlPath, $returnValue) $this->assertEquals($returnValue, $this->_helper->{$method}()); } + public function testHasSendCurrency() + { + $this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true); + + $this->assertTrue($this->_helper->hasSendCurrency()); + } + public function testGetConversionValueDynamic() { $returnValue = 4.1; - $returnValueCurrency = 'USD'; $this->_scopeConfigMock->expects( $this->any() )->method( @@ -219,6 +224,16 @@ public function testGetConversionValueDynamic() )->will( $this->returnValue($returnValue) ); + + + $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( @@ -229,7 +244,6 @@ public function testGetConversionValueDynamic() $this->returnValue($returnValueCurrency) ); - $this->assertEquals($returnValue, $this->_helper->getConversionValue()); $this->assertEquals($returnValueCurrency, $this->_helper->getConversionValueCurrency()); } diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php index 9170cb114cef2..1f9b2bfdf2556 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 { /** @@ -135,7 +137,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() @@ -146,20 +151,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 - ); - $this->_registryMock->expects( - $this->once() - )->method( - 'register' - )->with( - \Magento\GoogleAdwords\Helper\Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME, - $conversionCurrency + )->withConsecutive( + [ + Data::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME, + $conversionCurrency + ], + [ + Data::CONVERSION_VALUE_REGISTRY_NAME, + $conversionValue, + ] ); $this->assertSame($this->_model, $this->_model->execute($this->_eventObserverMock)); From 99fd6aa3f8d7cd5f5b1709a0a492ddf73f42a775 Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Fri, 18 Aug 2017 12:47:54 +0300 Subject: [PATCH 4/7] magento/magento2#10558: Option to send currency in Google Adwords when using dynamic value - Fixes per failed tests --- app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php index 500b0d822dc69..82058665a2abd 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php @@ -227,7 +227,6 @@ public function testGetConversionValueDynamic() $this->assertEquals($returnValue, $this->_helper->getConversionValue()); - } public function testGetConversionValueCurrency() From c1d42fcfa3b7c8f3c56fcf6b4c8d866999decead Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Fri, 18 Aug 2017 16:15:58 +0300 Subject: [PATCH 5/7] magento/magento2#10558: Option to send currency in Google Adwords when using dynamic value - Fixes per failed tests --- .../GoogleAdwords/Observer/SetConversionValueObserver.php | 8 ++++---- .../Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php index 51d5d6c5766dd..bfe77067b866c 100644 --- a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php +++ b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php @@ -61,11 +61,11 @@ public function execute(\Magento\Framework\Event\Observer $observer) $conversionValue = 0; $conversionCurrency = false; - $orderHasSendCurrency = $this->_helper->hasSendCurrency(); + $sendOrderCurrency = $this->_helper->hasSendCurrency(); foreach ($this->_collection as $order) { - /** @var $order \Magento\Sales\Model\Order */ - $conversionValue += $orderHasSendCurrency ? $order->getGrandTotal() : $order->getBaseGrandTotal(); - $conversionCurrency = $orderHasSendCurrency ? $order->getOrderCurrencyCode() : false; + /** @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, diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php index 82058665a2abd..92a9a5769a14f 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php @@ -225,7 +225,6 @@ public function testGetConversionValueDynamic() $this->returnValue($returnValue) ); - $this->assertEquals($returnValue, $this->_helper->getConversionValue()); } From 74e5671330495767ffacb862d12b8762eb8b25c6 Mon Sep 17 00:00:00 2001 From: DominicWatts Date: Fri, 18 Aug 2017 21:36:34 +0100 Subject: [PATCH 6/7] review requested changes --- app/code/Magento/GoogleAdwords/Helper/Data.php | 9 ++++++--- .../Observer/SetConversionValueObserver.php | 2 +- .../Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php | 4 ++-- .../Unit/Observer/SetConversionValueObserverTest.php | 2 +- .../GoogleAdwords/view/frontend/templates/code.phtml | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/GoogleAdwords/Helper/Data.php b/app/code/Magento/GoogleAdwords/Helper/Data.php index 9ef7f6217e03f..e4999ce7372b4 100644 --- a/app/code/Magento/GoogleAdwords/Helper/Data.php +++ b/app/code/Magento/GoogleAdwords/Helper/Data.php @@ -64,6 +64,9 @@ 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'; /**#@-*/ @@ -277,7 +280,7 @@ public function getConversionValue() * * @return boolean */ - public function hasSendCurrency() + public function hasSendConversionValueCurrency() { return $this->scopeConfig->isSetFlag( self::XML_PATH_SEND_CURRENCY, @@ -288,11 +291,11 @@ public function hasSendCurrency() /** * Get Google AdWords conversion value currency * - * @return float + * @return string|false */ public function getConversionValueCurrency() { - if ($this->hasSendCurrency()) { + 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 bfe77067b866c..d513fa3b3ad1e 100644 --- a/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php +++ b/app/code/Magento/GoogleAdwords/Observer/SetConversionValueObserver.php @@ -61,7 +61,7 @@ public function execute(\Magento\Framework\Event\Observer $observer) $conversionValue = 0; $conversionCurrency = false; - $sendOrderCurrency = $this->_helper->hasSendCurrency(); + $sendOrderCurrency = $this->_helper->hasSendConversionValueCurrency(); foreach ($this->_collection as $order) { /** @var $order \Magento\Sales\Api\Data\OrderInterface */ $conversionValue += $sendOrderCurrency ? $order->getGrandTotal() : $order->getBaseGrandTotal(); diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php index 92a9a5769a14f..e45ff12a9e206 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Helper/DataTest.php @@ -196,11 +196,11 @@ public function testGetStoreConfigValue($method, $xmlPath, $returnValue) $this->assertEquals($returnValue, $this->_helper->{$method}()); } - public function testHasSendCurrency() + public function testHasSendConversionValueCurrency() { $this->_scopeConfigMock->expects($this->once())->method('isSetFlag')->willReturn(true); - $this->assertTrue($this->_helper->hasSendCurrency()); + $this->assertTrue($this->_helper->hasSendConversionValueCurrency()); } public function testGetConversionValueDynamic() diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php index 1f9b2bfdf2556..e488f1b2c6826 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php @@ -127,7 +127,7 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds() $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('hasSendCurrency')->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() diff --git a/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml b/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml index 0360d9c5f78ca..7daad72994b00 100644 --- a/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml +++ b/app/code/Magento/GoogleAdwords/view/frontend/templates/code.phtml @@ -18,7 +18,7 @@ var google_conversion_color = "getHelper()->getConversionColor() ?>"; var google_conversion_label = "getHelper()->getConversionLabel() ?>"; var google_conversion_value = getHelper()->getConversionValue() ?>; - getHelper()->hasSendCurrency() && $block->getHelper()->getConversionValueCurrency()): ?> + getHelper()->hasSendConversionValueCurrency() && $block->getHelper()->getConversionValueCurrency()): ?> var google_conversion_currency = "getHelper()->getConversionValueCurrency() ?>"; /* ]]> */ From b65410e43aad4047bdb1a52f7a0acd9436c49405 Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Sat, 19 Aug 2017 18:29:42 +0300 Subject: [PATCH 7/7] magento/magento2#10558: Option to send currency in Google Adwords when using dynamic value - Fixes per failed tests --- .../Test/Unit/Observer/SetConversionValueObserverTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php index e488f1b2c6826..4e8d8e34fbea7 100644 --- a/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php +++ b/app/code/Magento/GoogleAdwords/Test/Unit/Observer/SetConversionValueObserverTest.php @@ -127,7 +127,8 @@ public function testSetConversionValueWhenAdwordsActiveWithOrdersIds() $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->_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()