Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0"?>
- <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
- <preference for="MagentoContactBlockContactForm" type="[namespace][modulename]BlockContactForm" />
- </config>
- <?php
- namespace [namespace][modulename]Block;
- use MagentoFrameworkViewElementTemplate;
- class ContactForm extends MagentoContactBlockContactForm
- {
- public function __construct(
- TemplateContext $context,
- MagentoDirectoryBlockData $directoryBlock,
- array $data = []
- )
- {
- parent::__construct($context, $data);
- $this->_isScopePrivate = true;
- $this->directoryBlock = $directoryBlock;
- }
- public function getCountries()
- {
- $country = $this->directoryBlock->getCountryHtmlSelect();
- return $country;
- }
- public function getRegion()
- {
- $region = $this->directoryBlock->getRegionHtmlSelect();
- return $region;
- }
- }
- <?php
- $countryList=$block->getCountries();
- $regionList=$block->getRegion();
- ?>
- <div class="field country">
- <label class="label" for="country"><span><?php echo __('Country') ?></span></label>
- <div class="control">
- <?php echo $countryList?>
- </div>
- </div>
- <div class="field country">
- <label class="label" for="region"><span><?php echo __('Region') ?></span></label>
- <div class="control">
- <?php echo $regionList?>
- </div>
- </div>
- $(document).on('change','#country',function() {
- var param = 'country='+$('#country').val();
- $.ajax({
- showLoader: true,
- url: YOUR_URL_HERE,
- data: param,
- type: "GET",
- dataType: 'json'
- }).done(function (data) {
- //data.value has the array of regions
- });
- });
- public function __construct(
- Context $context,
- MagentoFrameworkControllerResultJsonFactory $resultJsonFactory,
- MagentoDirectoryModelRegionFactory $regionColFactory,
- PageFactory $resultPageFactory
- ) {
- $this->regionColFactory = $regionColFactory;
- $this->resultJsonFactory = $resultJsonFactory;
- parent::__construct($context);
- }
- public function execute()
- {
- $this->_view->loadLayout();
- $this->_view->getLayout()->initMessages();
- $this->_view->renderLayout();
- $result = $this->resultJsonFactory->create();
- $regions=$this->regionColFactory->create()->getCollection()->addFieldToFilter('country_id',$this->getRequest()->getParam('country'));
- return $result->setData(['success' => true,'value'=>$regions->getData()]);
- }
- In Phtml
- <div class="field company required">
- <label class="label" for="company"><span><?php echo __('Company') ?></span></label>
- <div class="control">
- <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"/>
- </div>
- </div>
- <div class="field country required">
- <label class="label" for="country"><span><?php echo __('Country') ?></span></label>
- <div class="control">
- <?php echo $countryList?>
- </div>
- </div>
- <div class="field region required">
- <label class="label" for="region"><span><?php /* @escapeNotVerified */ echo __('State/Province') ?></span></label>
- <div class="control">
- <?php echo $regionList?>
- </div>
- </div>
- <script>
- require(["jquery"], function ($) {
- $(document).on('change','#country',function() {
- //alert($('#country').val());
- if(document.getElementById('states')){
- document.getElementById('states').value = '';
- }
- if(document.getElementById('state')){
- document.getElementById('state').value = '';
- }
- var param = 'country='+$('#country').val();
- $.ajax({
- showLoader: true,
- url: '<?php /* @escapeNotVerified */ echo $block->getCatalogrequest(); ?>',
- data: param,
- type: "GET",
- dataType: 'json'
- }).done(function (data) {
- if(data.value=='')
- {
- $('.field.states.required').show();
- $('.field.region.required').hide();
- }
- else
- {
- $('.field.states.required').hide();
- $('.field.region.required').show();
- $('#state').empty().append(data.value);
- }
- });
- });
- });
- </script>
Add Comment
Please, Sign In to add comment