Guest User

Untitled

a guest
Dec 12th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
  3. <preference for="MagentoContactBlockContactForm" type="[namespace][modulename]BlockContactForm" />
  4. </config>
  5.  
  6. <?php
  7.  
  8. namespace [namespace][modulename]Block;
  9.  
  10. use MagentoFrameworkViewElementTemplate;
  11.  
  12. class ContactForm extends MagentoContactBlockContactForm
  13. {
  14.  
  15. public function __construct(
  16. TemplateContext $context,
  17. MagentoDirectoryBlockData $directoryBlock,
  18. array $data = []
  19. )
  20. {
  21. parent::__construct($context, $data);
  22. $this->_isScopePrivate = true;
  23. $this->directoryBlock = $directoryBlock;
  24. }
  25.  
  26. public function getCountries()
  27. {
  28. $country = $this->directoryBlock->getCountryHtmlSelect();
  29. return $country;
  30. }
  31. public function getRegion()
  32. {
  33. $region = $this->directoryBlock->getRegionHtmlSelect();
  34. return $region;
  35. }
  36. }
  37.  
  38. <?php
  39. $countryList=$block->getCountries();
  40. $regionList=$block->getRegion();
  41. ?>
  42. <div class="field country">
  43. <label class="label" for="country"><span><?php echo __('Country') ?></span></label>
  44. <div class="control">
  45. <?php echo $countryList?>
  46. </div>
  47. </div>
  48. <div class="field country">
  49. <label class="label" for="region"><span><?php echo __('Region') ?></span></label>
  50. <div class="control">
  51. <?php echo $regionList?>
  52. </div>
  53. </div>
  54.  
  55. $(document).on('change','#country',function() {
  56. var param = 'country='+$('#country').val();
  57. $.ajax({
  58. showLoader: true,
  59. url: YOUR_URL_HERE,
  60. data: param,
  61. type: "GET",
  62. dataType: 'json'
  63. }).done(function (data) {
  64. //data.value has the array of regions
  65. });
  66. });
  67.  
  68. public function __construct(
  69. Context $context,
  70. MagentoFrameworkControllerResultJsonFactory $resultJsonFactory,
  71. MagentoDirectoryModelRegionFactory $regionColFactory,
  72. PageFactory $resultPageFactory
  73. ) {
  74. $this->regionColFactory = $regionColFactory;
  75. $this->resultJsonFactory = $resultJsonFactory;
  76. parent::__construct($context);
  77. }
  78.  
  79. public function execute()
  80. {
  81. $this->_view->loadLayout();
  82. $this->_view->getLayout()->initMessages();
  83. $this->_view->renderLayout();
  84.  
  85. $result = $this->resultJsonFactory->create();
  86. $regions=$this->regionColFactory->create()->getCollection()->addFieldToFilter('country_id',$this->getRequest()->getParam('country'));
  87. return $result->setData(['success' => true,'value'=>$regions->getData()]);
  88.  
  89. }
  90.  
  91. In Phtml
  92. <div class="field company required">
  93. <label class="label" for="company"><span><?php echo __('Company') ?></span></label>
  94. <div class="control">
  95. <input name="company" id="company" title="<?php echo __('company') ?>" value="" class="input-text letters-only" type="text" data-validate="{required:true,'letters-only':true}" style="width:400px"/>
  96. </div>
  97. </div>
  98.  
  99. <div class="field country required">
  100. <label class="label" for="country"><span><?php echo __('Country') ?></span></label>
  101. <div class="control">
  102. <?php echo $countryList?>
  103. </div>
  104. </div>
  105.  
  106.  
  107. <div class="field region required">
  108. <label class="label" for="region"><span><?php /* @escapeNotVerified */ echo __('State/Province') ?></span></label>
  109. <div class="control">
  110. <?php echo $regionList?>
  111. </div>
  112. </div>
  113. <script>
  114. require(["jquery"], function ($) {
  115. $(document).on('change','#country',function() {
  116. //alert($('#country').val());
  117. if(document.getElementById('states')){
  118. document.getElementById('states').value = '';
  119. }
  120. if(document.getElementById('state')){
  121. document.getElementById('state').value = '';
  122. }
  123. var param = 'country='+$('#country').val();
  124. $.ajax({
  125. showLoader: true,
  126. url: '<?php /* @escapeNotVerified */ echo $block->getCatalogrequest(); ?>',
  127. data: param,
  128. type: "GET",
  129. dataType: 'json'
  130. }).done(function (data) {
  131.  
  132. if(data.value=='')
  133. {
  134. $('.field.states.required').show();
  135. $('.field.region.required').hide();
  136. }
  137. else
  138. {
  139. $('.field.states.required').hide();
  140. $('.field.region.required').show();
  141. $('#state').empty().append(data.value);
  142. }
  143. });
  144. });
  145. });
  146. </script>
Add Comment
Please, Sign In to add comment