Allmethods.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Model\Config\Source;
  7. class Allmethods implements \Magento\Framework\Option\ArrayInterface
  8. {
  9. /**
  10. * Core store config
  11. *
  12. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  13. */
  14. protected $_scopeConfig;
  15. /**
  16. * @var \Magento\Shipping\Model\Config
  17. */
  18. protected $_shippingConfig;
  19. /**
  20. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  21. * @param \Magento\Shipping\Model\Config $shippingConfig
  22. */
  23. public function __construct(
  24. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  25. \Magento\Shipping\Model\Config $shippingConfig
  26. ) {
  27. $this->_scopeConfig = $scopeConfig;
  28. $this->_shippingConfig = $shippingConfig;
  29. }
  30. /**
  31. * Return array of carriers.
  32. * If $isActiveOnlyFlag is set to true, will return only active carriers
  33. *
  34. * @param bool $isActiveOnlyFlag
  35. * @return array
  36. */
  37. public function toOptionArray($isActiveOnlyFlag = false)
  38. {
  39. $methods = [['value' => '', 'label' => '']];
  40. $carriers = $this->_shippingConfig->getAllCarriers();
  41. foreach ($carriers as $carrierCode => $carrierModel) {
  42. if (!$carrierModel->isActive() && (bool)$isActiveOnlyFlag === true) {
  43. continue;
  44. }
  45. $carrierMethods = $carrierModel->getAllowedMethods();
  46. if (!$carrierMethods) {
  47. continue;
  48. }
  49. $carrierTitle = $this->_scopeConfig->getValue(
  50. 'carriers/' . $carrierCode . '/title',
  51. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  52. );
  53. $methods[$carrierCode] = ['label' => $carrierTitle, 'value' => []];
  54. foreach ($carrierMethods as $methodCode => $methodTitle) {
  55. $methods[$carrierCode]['value'][] = [
  56. 'value' => $carrierCode . '_' . $methodCode,
  57. 'label' => '[' . $carrierCode . '] ' . $methodTitle,
  58. ];
  59. }
  60. }
  61. return $methods;
  62. }
  63. }