123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- /**
- * Google AdWords Data Helper
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\GoogleAdwords\Helper;
- /**
- * @api
- * @since 100.0.2
- */
- class Data extends \Magento\Framework\App\Helper\AbstractHelper
- {
- /**#@+
- * Google AdWords language codes
- */
- const XML_PATH_LANGUAGES = 'google/adwords/languages';
- const XML_PATH_LANGUAGE_CONVERT = 'google/adwords/language_convert';
- /**#@-*/
- /**#@+
- * Google AdWords conversion src
- */
- const XML_PATH_CONVERSION_JS_SRC = 'google/adwords/conversion_js_src';
- const XML_PATH_CONVERSION_IMG_SRC = 'google/adwords/conversion_img_src';
- /**#@-*/
- /**
- * Google AdWords registry name for conversion value
- */
- 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
- */
- const CONVERSION_VALUE_DEFAULT = 0;
- /**#@+
- * Google AdWords config data
- */
- const XML_PATH_ACTIVE = 'google/adwords/active';
- const XML_PATH_CONVERSION_ID = 'google/adwords/conversion_id';
- const XML_PATH_CONVERSION_LANGUAGE = 'google/adwords/conversion_language';
- const XML_PATH_CONVERSION_FORMAT = 'google/adwords/conversion_format';
- const XML_PATH_CONVERSION_COLOR = 'google/adwords/conversion_color';
- const XML_PATH_CONVERSION_LABEL = 'google/adwords/conversion_label';
- const XML_PATH_CONVERSION_VALUE_TYPE = 'google/adwords/conversion_value_type';
- 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';
- /**#@-*/
- /**#@+
- * Conversion value types
- */
- const CONVERSION_VALUE_TYPE_DYNAMIC = 1;
- const CONVERSION_VALUE_TYPE_CONSTANT = 0;
- /**#@-*/
- /**#@-*/
- protected $_registry;
- /**
- * @param \Magento\Framework\App\Helper\Context $context
- * @param \Magento\Framework\Registry $registry
- */
- public function __construct(
- \Magento\Framework\App\Helper\Context $context,
- \Magento\Framework\Registry $registry
- ) {
- parent::__construct($context);
- $this->_registry = $registry;
- }
- /**
- * Is Google AdWords active
- *
- * @return bool
- */
- public function isGoogleAdwordsActive()
- {
- return $this->scopeConfig->isSetFlag(
- self::XML_PATH_ACTIVE,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- ) &&
- $this->getConversionId() &&
- $this->getConversionLanguage() &&
- $this->getConversionFormat() &&
- $this->getConversionColor() &&
- $this->getConversionLabel();
- }
- /**
- * Retrieve language codes from config
- *
- * @return string[]
- */
- public function getLanguageCodes()
- {
- return (array)$this->scopeConfig->getValue(self::XML_PATH_LANGUAGES, 'default');
- }
- /**
- * Convert language code in the code of the current locale language
- *
- * @param string $language
- * @return string
- */
- public function convertLanguageCodeToLocaleCode($language)
- {
- $convertArray = (array)$this->scopeConfig->getValue(self::XML_PATH_LANGUAGE_CONVERT, 'default');
- return isset($convertArray[$language]) ? $convertArray[$language] : $language;
- }
- /**
- * Get conversion path to js src
- *
- * @return string
- */
- public function getConversionJsSrc()
- {
- return (string)$this->scopeConfig->getValue(self::XML_PATH_CONVERSION_JS_SRC, 'default');
- }
- /**
- * Get conversion img src
- *
- * @return string
- */
- public function getConversionImgSrc()
- {
- return sprintf(
- $this->scopeConfig->getValue(self::XML_PATH_CONVERSION_IMG_SRC, 'default'),
- $this->getConversionId(),
- $this->getConversionLabel()
- );
- }
- /**
- * Get Google AdWords conversion id
- *
- * @return int
- */
- public function getConversionId()
- {
- return (int)$this->scopeConfig->getValue(
- self::XML_PATH_CONVERSION_ID,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Get Google AdWords conversion language
- *
- * @return string
- */
- public function getConversionLanguage()
- {
- return $this->scopeConfig->getValue(
- self::XML_PATH_CONVERSION_LANGUAGE,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Get Google AdWords conversion format
- *
- * @return int
- */
- public function getConversionFormat()
- {
- return $this->scopeConfig->getValue(
- self::XML_PATH_CONVERSION_FORMAT,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Get Google AdWords conversion color
- *
- * @return string
- */
- public function getConversionColor()
- {
- return $this->scopeConfig->getValue(
- self::XML_PATH_CONVERSION_COLOR,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Get Google AdWords conversion label
- *
- * @return string
- */
- public function getConversionLabel()
- {
- return $this->scopeConfig->getValue(
- self::XML_PATH_CONVERSION_LABEL,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Get Google AdWords conversion value type
- *
- * @return string
- */
- public function getConversionValueType()
- {
- return $this->scopeConfig->getValue(
- self::XML_PATH_CONVERSION_VALUE_TYPE,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Checks if conversion value is dynamic
- *
- * @return bool
- */
- public function isDynamicConversionValue()
- {
- return $this->getConversionValueType() == self::CONVERSION_VALUE_TYPE_DYNAMIC;
- }
- /**
- * Get Google AdWords conversion value constant
- *
- * @return float
- */
- public function getConversionValueConstant()
- {
- return (double)$this->scopeConfig->getValue(
- self::XML_PATH_CONVERSION_VALUE,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- /**
- * Get Google AdWords conversion value
- *
- * @return float
- */
- public function getConversionValue()
- {
- if ($this->isDynamicConversionValue()) {
- $conversionValue = (double)$this->_registry->registry(self::CONVERSION_VALUE_REGISTRY_NAME);
- } else {
- $conversionValue = $this->getConversionValueConstant();
- }
- return empty($conversionValue) ? self::CONVERSION_VALUE_DEFAULT : $conversionValue;
- }
- /**
- * Get send order currency to Google Adwords
- *
- * @return boolean
- * @since 100.3.0
- */
- 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
- * @since 100.3.0
- */
- public function getConversionValueCurrency()
- {
- if ($this->hasSendConversionValueCurrency()) {
- return (string) $this->_registry->registry(self::CONVERSION_VALUE_CURRENCY_REGISTRY_NAME);
- }
- return false;
- }
- }
|