Config.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Model;
  7. use Magento\Shipping\Model\Carrier\AbstractCarrierInterface;
  8. /**
  9. * Class Config
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Config extends \Magento\Framework\DataObject
  14. {
  15. /**
  16. * Shipping origin settings
  17. */
  18. const XML_PATH_ORIGIN_COUNTRY_ID = 'shipping/origin/country_id';
  19. const XML_PATH_ORIGIN_REGION_ID = 'shipping/origin/region_id';
  20. const XML_PATH_ORIGIN_CITY = 'shipping/origin/city';
  21. const XML_PATH_ORIGIN_POSTCODE = 'shipping/origin/postcode';
  22. /**
  23. * Core store config
  24. *
  25. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  26. */
  27. protected $_scopeConfig;
  28. /**
  29. * @var \Magento\Shipping\Model\CarrierFactory
  30. */
  31. protected $_carrierFactory;
  32. /**
  33. * Constructor
  34. *
  35. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  36. * @param \Magento\Shipping\Model\CarrierFactory $carrierFactory
  37. * @param array $data
  38. */
  39. public function __construct(
  40. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  41. \Magento\Shipping\Model\CarrierFactory $carrierFactory,
  42. array $data = []
  43. ) {
  44. $this->_scopeConfig = $scopeConfig;
  45. $this->_carrierFactory = $carrierFactory;
  46. parent::__construct($data);
  47. }
  48. /**
  49. * Retrieve active system carriers
  50. *
  51. * @param mixed $store
  52. * @return AbstractCarrierInterface[]
  53. */
  54. public function getActiveCarriers($store = null)
  55. {
  56. $carriers = [];
  57. $config = $this->_scopeConfig->getValue('carriers', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
  58. foreach (array_keys($config) as $carrierCode) {
  59. if ($this->_scopeConfig->isSetFlag(
  60. 'carriers/' . $carrierCode . '/active',
  61. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  62. $store
  63. )) {
  64. $carrierModel = $this->_carrierFactory->create($carrierCode, $store);
  65. if ($carrierModel) {
  66. $carriers[$carrierCode] = $carrierModel;
  67. }
  68. }
  69. }
  70. return $carriers;
  71. }
  72. /**
  73. * Retrieve all system carriers
  74. *
  75. * @param mixed $store
  76. * @return AbstractCarrierInterface[]
  77. */
  78. public function getAllCarriers($store = null)
  79. {
  80. $carriers = [];
  81. $config = $this->_scopeConfig->getValue('carriers', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
  82. foreach (array_keys($config) as $carrierCode) {
  83. $model = $this->_carrierFactory->create($carrierCode, $store);
  84. if ($model) {
  85. $carriers[$carrierCode] = $model;
  86. }
  87. }
  88. return $carriers;
  89. }
  90. }