Carrier.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Helper;
  7. /**
  8. * Carrier helper
  9. */
  10. class Carrier extends \Magento\Framework\App\Helper\AbstractHelper
  11. {
  12. /**
  13. * Carriers root xml path
  14. */
  15. const XML_PATH_CARRIERS_ROOT = 'carriers';
  16. /**
  17. * Config path to UE country list
  18. */
  19. const XML_PATH_EU_COUNTRIES_LIST = 'general/country/eu_countries';
  20. /**
  21. * Locale interface
  22. *
  23. * @var \Magento\Framework\Locale\ResolverInterface $localeResolver
  24. */
  25. protected $localeResolver;
  26. /**
  27. * @param \Magento\Framework\App\Helper\Context $context
  28. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  29. */
  30. public function __construct(
  31. \Magento\Framework\App\Helper\Context $context,
  32. \Magento\Framework\Locale\ResolverInterface $localeResolver
  33. ) {
  34. $this->localeResolver = $localeResolver;
  35. parent::__construct($context);
  36. }
  37. /**
  38. * Get online shipping carrier codes
  39. *
  40. * @param int|\Magento\Store\Model\Store|null $store
  41. * @return array
  42. */
  43. public function getOnlineCarrierCodes($store = null)
  44. {
  45. $carriersCodes = [];
  46. foreach ($this->scopeConfig->getValue(
  47. self::XML_PATH_CARRIERS_ROOT,
  48. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  49. $store
  50. ) as $carrierCode => $carrier) {
  51. if (isset($carrier['is_online']) && $carrier['is_online']) {
  52. $carriersCodes[] = $carrierCode;
  53. }
  54. }
  55. return $carriersCodes;
  56. }
  57. /**
  58. * Get shipping carrier config value
  59. *
  60. * @param string $carrierCode
  61. * @param string $configPath
  62. * @param null $store
  63. * @return string
  64. */
  65. public function getCarrierConfigValue($carrierCode, $configPath, $store = null)
  66. {
  67. return $this->scopeConfig->getValue(
  68. sprintf('%s/%s/%s', self::XML_PATH_CARRIERS_ROOT, $carrierCode, $configPath),
  69. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  70. $store
  71. );
  72. }
  73. /**
  74. * Convert weight in different measure types
  75. *
  76. * @param int|float $value
  77. * @param string $sourceWeightMeasure
  78. * @param string $toWeightMeasure
  79. * @return int|null|string
  80. */
  81. public function convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure)
  82. {
  83. if ($value) {
  84. $locale = $this->localeResolver->getLocale();
  85. $unitWeight = new \Zend_Measure_Weight($value, $sourceWeightMeasure, $locale);
  86. $unitWeight->setType($toWeightMeasure);
  87. return $unitWeight->getValue();
  88. }
  89. return null;
  90. }
  91. /**
  92. * Convert dimensions in different measure types
  93. *
  94. * @param int|float $value
  95. * @param string $sourceDimensionMeasure
  96. * @param string $toDimensionMeasure
  97. * @return int|null|string
  98. */
  99. public function convertMeasureDimension($value, $sourceDimensionMeasure, $toDimensionMeasure)
  100. {
  101. if ($value) {
  102. $locale = $this->localeResolver->getLocale();
  103. $unitDimension = new \Zend_Measure_Length($value, $sourceDimensionMeasure, $locale);
  104. $unitDimension->setType($toDimensionMeasure);
  105. return $unitDimension->getValue();
  106. }
  107. return null;
  108. }
  109. /**
  110. * Get name of measure by its type
  111. *
  112. * @param string $key
  113. * @return string
  114. */
  115. public function getMeasureWeightName($key)
  116. {
  117. $weight = new \Zend_Measure_Weight(0);
  118. $conversionList = $weight->getConversionList();
  119. if (!empty($conversionList[$key]) && !empty($conversionList[$key][1])) {
  120. return $conversionList[$key][1];
  121. }
  122. return '';
  123. }
  124. /**
  125. * Get name of measure by its type
  126. *
  127. * @param string $key
  128. * @return string
  129. */
  130. public function getMeasureDimensionName($key)
  131. {
  132. $weight = new \Zend_Measure_Length(0);
  133. $conversionList = $weight->getConversionList();
  134. if (!empty($conversionList[$key]) && !empty($conversionList[$key][1])) {
  135. return $conversionList[$key][1];
  136. }
  137. return '';
  138. }
  139. /**
  140. * Check whether specified country is in EU countries list
  141. *
  142. * @param string $countryCode
  143. * @param null|int $storeId
  144. * @return bool
  145. */
  146. public function isCountryInEU($countryCode, $storeId = null)
  147. {
  148. $euCountries = explode(
  149. ',',
  150. $this->scopeConfig->getValue(
  151. self::XML_PATH_EU_COUNTRIES_LIST,
  152. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  153. $storeId
  154. )
  155. );
  156. return in_array($countryCode, $euCountries);
  157. }
  158. }