Data.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * Google AdWords Data Helper
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\GoogleAdwords\Helper;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  14. {
  15. /**#@+
  16. * Google AdWords language codes
  17. */
  18. const XML_PATH_LANGUAGES = 'google/adwords/languages';
  19. const XML_PATH_LANGUAGE_CONVERT = 'google/adwords/language_convert';
  20. /**#@-*/
  21. /**#@+
  22. * Google AdWords conversion src
  23. */
  24. const XML_PATH_CONVERSION_JS_SRC = 'google/adwords/conversion_js_src';
  25. const XML_PATH_CONVERSION_IMG_SRC = 'google/adwords/conversion_img_src';
  26. /**#@-*/
  27. /**
  28. * Google AdWords registry name for conversion value
  29. */
  30. const CONVERSION_VALUE_REGISTRY_NAME = 'google_adwords_conversion_value';
  31. /**
  32. * Google AdWords registry name for conversion value currency
  33. */
  34. const CONVERSION_VALUE_CURRENCY_REGISTRY_NAME = 'google_adwords_conversion_value_currency';
  35. /**
  36. * Default value for conversion value
  37. */
  38. const CONVERSION_VALUE_DEFAULT = 0;
  39. /**#@+
  40. * Google AdWords config data
  41. */
  42. const XML_PATH_ACTIVE = 'google/adwords/active';
  43. const XML_PATH_CONVERSION_ID = 'google/adwords/conversion_id';
  44. const XML_PATH_CONVERSION_LANGUAGE = 'google/adwords/conversion_language';
  45. const XML_PATH_CONVERSION_FORMAT = 'google/adwords/conversion_format';
  46. const XML_PATH_CONVERSION_COLOR = 'google/adwords/conversion_color';
  47. const XML_PATH_CONVERSION_LABEL = 'google/adwords/conversion_label';
  48. const XML_PATH_CONVERSION_VALUE_TYPE = 'google/adwords/conversion_value_type';
  49. const XML_PATH_CONVERSION_VALUE = 'google/adwords/conversion_value';
  50. /**
  51. * Google Adwords send order conversion value currency when using dynamic value
  52. */
  53. const XML_PATH_SEND_CURRENCY = 'google/adwords/send_currency';
  54. /**#@-*/
  55. /**#@+
  56. * Conversion value types
  57. */
  58. const CONVERSION_VALUE_TYPE_DYNAMIC = 1;
  59. const CONVERSION_VALUE_TYPE_CONSTANT = 0;
  60. /**#@-*/
  61. /**#@-*/
  62. protected $_registry;
  63. /**
  64. * @param \Magento\Framework\App\Helper\Context $context
  65. * @param \Magento\Framework\Registry $registry
  66. */
  67. public function __construct(
  68. \Magento\Framework\App\Helper\Context $context,
  69. \Magento\Framework\Registry $registry
  70. ) {
  71. parent::__construct($context);
  72. $this->_registry = $registry;
  73. }
  74. /**
  75. * Is Google AdWords active
  76. *
  77. * @return bool
  78. */
  79. public function isGoogleAdwordsActive()
  80. {
  81. return $this->scopeConfig->isSetFlag(
  82. self::XML_PATH_ACTIVE,
  83. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  84. ) &&
  85. $this->getConversionId() &&
  86. $this->getConversionLanguage() &&
  87. $this->getConversionFormat() &&
  88. $this->getConversionColor() &&
  89. $this->getConversionLabel();
  90. }
  91. /**
  92. * Retrieve language codes from config
  93. *
  94. * @return string[]
  95. */
  96. public function getLanguageCodes()
  97. {
  98. return (array)$this->scopeConfig->getValue(self::XML_PATH_LANGUAGES, 'default');
  99. }
  100. /**
  101. * Convert language code in the code of the current locale language
  102. *
  103. * @param string $language
  104. * @return string
  105. */
  106. public function convertLanguageCodeToLocaleCode($language)
  107. {
  108. $convertArray = (array)$this->scopeConfig->getValue(self::XML_PATH_LANGUAGE_CONVERT, 'default');
  109. return isset($convertArray[$language]) ? $convertArray[$language] : $language;
  110. }
  111. /**
  112. * Get conversion path to js src
  113. *
  114. * @return string
  115. */
  116. public function getConversionJsSrc()
  117. {
  118. return (string)$this->scopeConfig->getValue(self::XML_PATH_CONVERSION_JS_SRC, 'default');
  119. }
  120. /**
  121. * Get conversion img src
  122. *
  123. * @return string
  124. */
  125. public function getConversionImgSrc()
  126. {
  127. return sprintf(
  128. $this->scopeConfig->getValue(self::XML_PATH_CONVERSION_IMG_SRC, 'default'),
  129. $this->getConversionId(),
  130. $this->getConversionLabel()
  131. );
  132. }
  133. /**
  134. * Get Google AdWords conversion id
  135. *
  136. * @return int
  137. */
  138. public function getConversionId()
  139. {
  140. return (int)$this->scopeConfig->getValue(
  141. self::XML_PATH_CONVERSION_ID,
  142. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  143. );
  144. }
  145. /**
  146. * Get Google AdWords conversion language
  147. *
  148. * @return string
  149. */
  150. public function getConversionLanguage()
  151. {
  152. return $this->scopeConfig->getValue(
  153. self::XML_PATH_CONVERSION_LANGUAGE,
  154. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  155. );
  156. }
  157. /**
  158. * Get Google AdWords conversion format
  159. *
  160. * @return int
  161. */
  162. public function getConversionFormat()
  163. {
  164. return $this->scopeConfig->getValue(
  165. self::XML_PATH_CONVERSION_FORMAT,
  166. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  167. );
  168. }
  169. /**
  170. * Get Google AdWords conversion color
  171. *
  172. * @return string
  173. */
  174. public function getConversionColor()
  175. {
  176. return $this->scopeConfig->getValue(
  177. self::XML_PATH_CONVERSION_COLOR,
  178. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  179. );
  180. }
  181. /**
  182. * Get Google AdWords conversion label
  183. *
  184. * @return string
  185. */
  186. public function getConversionLabel()
  187. {
  188. return $this->scopeConfig->getValue(
  189. self::XML_PATH_CONVERSION_LABEL,
  190. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  191. );
  192. }
  193. /**
  194. * Get Google AdWords conversion value type
  195. *
  196. * @return string
  197. */
  198. public function getConversionValueType()
  199. {
  200. return $this->scopeConfig->getValue(
  201. self::XML_PATH_CONVERSION_VALUE_TYPE,
  202. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  203. );
  204. }
  205. /**
  206. * Checks if conversion value is dynamic
  207. *
  208. * @return bool
  209. */
  210. public function isDynamicConversionValue()
  211. {
  212. return $this->getConversionValueType() == self::CONVERSION_VALUE_TYPE_DYNAMIC;
  213. }
  214. /**
  215. * Get Google AdWords conversion value constant
  216. *
  217. * @return float
  218. */
  219. public function getConversionValueConstant()
  220. {
  221. return (double)$this->scopeConfig->getValue(
  222. self::XML_PATH_CONVERSION_VALUE,
  223. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  224. );
  225. }
  226. /**
  227. * Get Google AdWords conversion value
  228. *
  229. * @return float
  230. */
  231. public function getConversionValue()
  232. {
  233. if ($this->isDynamicConversionValue()) {
  234. $conversionValue = (double)$this->_registry->registry(self::CONVERSION_VALUE_REGISTRY_NAME);
  235. } else {
  236. $conversionValue = $this->getConversionValueConstant();
  237. }
  238. return empty($conversionValue) ? self::CONVERSION_VALUE_DEFAULT : $conversionValue;
  239. }
  240. /**
  241. * Get send order currency to Google Adwords
  242. *
  243. * @return boolean
  244. * @since 100.3.0
  245. */
  246. public function hasSendConversionValueCurrency()
  247. {
  248. return $this->scopeConfig->isSetFlag(
  249. self::XML_PATH_SEND_CURRENCY,
  250. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  251. );
  252. }
  253. /**
  254. * Get Google AdWords conversion value currency
  255. *
  256. * @return string|false
  257. * @since 100.3.0
  258. */
  259. public function getConversionValueCurrency()
  260. {
  261. if ($this->hasSendConversionValueCurrency()) {
  262. return (string) $this->_registry->registry(self::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME);
  263. }
  264. return false;
  265. }
  266. }