DeliveryOptionsConfigProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Delivery;
  6. use Magento\Checkout\Model\ConfigProviderInterface;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Store\Model\StoreManagerInterface;
  9. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  10. /**
  11. * Provide Delivery Options Data to Checkout.
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class DeliveryOptionsConfigProvider implements ConfigProviderInterface
  19. {
  20. /**
  21. * @var ModuleConfigInterface
  22. */
  23. private $moduleConfig;
  24. /**
  25. * @var StoreManagerInterface
  26. */
  27. private $storeManager;
  28. /**
  29. * DeliveryOptionsConfigProvider constructor.
  30. * @param ModuleConfigInterface $moduleConfig
  31. * @param StoreManagerInterface $storeManager
  32. */
  33. public function __construct(
  34. ModuleConfigInterface $moduleConfig,
  35. StoreManagerInterface $storeManager
  36. ) {
  37. $this->moduleConfig = $moduleConfig;
  38. $this->storeManager = $storeManager;
  39. }
  40. /**
  41. * Obtain country data for display in checkout, shipping method step.
  42. *
  43. * @return string[]
  44. */
  45. public function getConfig()
  46. {
  47. try {
  48. $storeId = $this->storeManager->getStore()->getId();
  49. } catch (NoSuchEntityException $exception) {
  50. $storeId = null;
  51. }
  52. if (!$this->moduleConfig->isEnabled($storeId)
  53. || (!$this->moduleConfig->isClickAndCollectEnabled($storeId)
  54. && !$this->moduleConfig->isCollectionPointsEnabled($storeId)
  55. )
  56. ) {
  57. return ['delivery-options' => []];
  58. }
  59. $deliveryOptions = $this->getDeliveryOptions();
  60. if (!$this->moduleConfig->isClickAndCollectEnabled($storeId)) {
  61. unset($deliveryOptions['to-pickup-store']);
  62. }
  63. if (!$this->moduleConfig->isCollectionPointsEnabled($storeId)) {
  64. unset($deliveryOptions['to-collection-point']);
  65. }
  66. return ['delivery-options' => array_values($deliveryOptions)];
  67. }
  68. /**
  69. * @return string[][]
  70. */
  71. private function getDeliveryOptions()
  72. {
  73. $deliveryOptions = [
  74. 'to-address' => [
  75. 'id' => 'to-address',
  76. 'label' => 'Send To Address',
  77. 'value' => 'toAddress'
  78. ],
  79. 'to-collection-point' => [
  80. 'id' => 'to-collection-point',
  81. 'label' => 'Send To Collection Point',
  82. 'value' => 'toCollectionPoint'
  83. ],
  84. 'to-pickup-store' => [
  85. 'id' => 'to-pickup-store',
  86. 'label' => 'Pick up in Store',
  87. 'value' => 'clickAndCollect'
  88. ]
  89. ];
  90. return $deliveryOptions;
  91. }
  92. }