LayoutProcessor.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Block\Cart;
  7. class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
  8. {
  9. /**
  10. * @var \Magento\Checkout\Block\Checkout\AttributeMerger
  11. */
  12. protected $merger;
  13. /**
  14. * @var \Magento\Directory\Model\ResourceModel\Country\Collection
  15. */
  16. protected $countryCollection;
  17. /**
  18. * @var \Magento\Directory\Model\ResourceModel\Region\Collection
  19. */
  20. protected $regionCollection;
  21. /**
  22. * @var \Magento\Customer\Api\Data\AddressInterface
  23. */
  24. protected $defaultShippingAddress = null;
  25. /**
  26. * @var \Magento\Directory\Model\TopDestinationCountries
  27. */
  28. private $topDestinationCountries;
  29. /**
  30. * @param \Magento\Checkout\Block\Checkout\AttributeMerger $merger
  31. * @param \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection
  32. * @param \Magento\Directory\Model\ResourceModel\Region\Collection $regionCollection
  33. * @param \Magento\Directory\Model\TopDestinationCountries $topDestinationCountries
  34. * @codeCoverageIgnore
  35. */
  36. public function __construct(
  37. \Magento\Checkout\Block\Checkout\AttributeMerger $merger,
  38. \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection,
  39. \Magento\Directory\Model\ResourceModel\Region\Collection $regionCollection,
  40. \Magento\Directory\Model\TopDestinationCountries $topDestinationCountries = null
  41. ) {
  42. $this->merger = $merger;
  43. $this->countryCollection = $countryCollection;
  44. $this->regionCollection = $regionCollection;
  45. $this->topDestinationCountries = $topDestinationCountries ?:
  46. \Magento\Framework\App\ObjectManager::getInstance()
  47. ->get(\Magento\Directory\Model\TopDestinationCountries::class);
  48. }
  49. /**
  50. * Show City in Shipping Estimation
  51. *
  52. * @return bool
  53. * @codeCoverageIgnore
  54. */
  55. protected function isCityActive()
  56. {
  57. return false;
  58. }
  59. /**
  60. * Show State in Shipping Estimation
  61. *
  62. * @return bool
  63. * @codeCoverageIgnore
  64. */
  65. protected function isStateActive()
  66. {
  67. return false;
  68. }
  69. /**
  70. * Process js Layout of block
  71. *
  72. * @param array $jsLayout
  73. * @return array
  74. * @SuppressWarnings(PHPMD.NPathComplexity)
  75. */
  76. public function process($jsLayout)
  77. {
  78. $elements = [
  79. 'city' => [
  80. 'visible' => $this->isCityActive(),
  81. 'formElement' => 'input',
  82. 'label' => __('City'),
  83. 'value' => null
  84. ],
  85. 'country_id' => [
  86. 'visible' => true,
  87. 'formElement' => 'select',
  88. 'label' => __('Country'),
  89. 'options' => [],
  90. 'value' => null
  91. ],
  92. 'region_id' => [
  93. 'visible' => true,
  94. 'formElement' => 'select',
  95. 'label' => __('State/Province'),
  96. 'options' => [],
  97. 'value' => null
  98. ],
  99. 'postcode' => [
  100. 'visible' => true,
  101. 'formElement' => 'input',
  102. 'label' => __('Zip/Postal Code'),
  103. 'value' => null
  104. ]
  105. ];
  106. if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
  107. $jsLayout['components']['checkoutProvider']['dictionaries'] = [
  108. 'country_id' => $this->countryCollection->loadByStore()->setForegroundCountries(
  109. $this->topDestinationCountries->getTopDestinations()
  110. )->toOptionArray(),
  111. 'region_id' => $this->regionCollection->addAllowedCountriesFilter()->toOptionArray(),
  112. ];
  113. }
  114. if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']
  115. ['address-fieldsets']['children'])
  116. ) {
  117. $fieldSetPointer = &$jsLayout['components']['block-summary']['children']['block-shipping']
  118. ['children']['address-fieldsets']['children'];
  119. $fieldSetPointer = $this->merger->merge($elements, 'checkoutProvider', 'shippingAddress', $fieldSetPointer);
  120. $fieldSetPointer['region_id']['config']['skipValidation'] = true;
  121. }
  122. return $jsLayout;
  123. }
  124. }