CheckoutFields.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\CustomerData;
  6. use Magento\Customer\CustomerData\SectionSourceInterface;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Store\Model\StoreManagerInterface;
  9. use Temando\Shipping\Model\Checkout\Schema\CheckoutField;
  10. use Temando\Shipping\Model\Checkout\Schema\CheckoutFieldsSchema;
  11. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  12. /**
  13. * CheckoutFields
  14. *
  15. * @package Temando\Shipping\CustomerData
  16. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  17. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  18. * @link http://www.temando.com/
  19. */
  20. class CheckoutFields implements SectionSourceInterface
  21. {
  22. /**
  23. * @var ModuleConfigInterface
  24. */
  25. private $moduleConfig;
  26. /**
  27. * @var CheckoutFieldsSchema
  28. */
  29. private $schema;
  30. /**
  31. * @var StoreManagerInterface
  32. */
  33. private $storeManager;
  34. /**
  35. * @param ModuleConfigInterface $moduleConfig
  36. * @param CheckoutFieldsSchema $schema
  37. * @param StoreManagerInterface $storeManager
  38. */
  39. public function __construct(
  40. ModuleConfigInterface $moduleConfig,
  41. CheckoutFieldsSchema $schema,
  42. StoreManagerInterface $storeManager
  43. ) {
  44. $this->moduleConfig = $moduleConfig;
  45. $this->schema = $schema;
  46. $this->storeManager = $storeManager;
  47. }
  48. /**
  49. * Obtain fields data for display in checkout, shipping method step
  50. *
  51. * @return string[]
  52. */
  53. public function getSectionData()
  54. {
  55. try {
  56. $storeId = $this->storeManager->getStore()->getId();
  57. } catch (NoSuchEntityException $exception) {
  58. $storeId = null;
  59. }
  60. if (!$this->moduleConfig->isEnabled($storeId)) {
  61. return ['fields' => []];
  62. }
  63. $fields = array_map(function (CheckoutField $field) {
  64. return $field->toArray();
  65. }, $this->schema->getFields());
  66. return ['fields' => $fields];
  67. }
  68. }