Magento – Magento 2 – State/Province dropdown and Country Dropdown in Contact form

contact-formcountry-regionsformsmagento2state

I need to add these 2 dropdowns to the contact form. How would I pull in those values for each dropdown?

Best Answer

Following method will add Country and Region dropdown to contact us form,

[namespace]/[modulename]/etc/di.xml

<?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="Magento\Contact\Block\ContactForm" type="[namespace]\[modulename]\Block\ContactForm" />
</config>

[namespace]/[modulename]/Block/ContactForm

<?php

namespace [namespace]\[modulename]\Block;

use Magento\Framework\View\Element\Template;

class ContactForm extends \Magento\Contact\Block\ContactForm
{

    public function __construct(
            Template\Context $context,
            \Magento\Directory\Block\Data $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;
    }
}

[namespace]/[modulename]/view/frontend/templates/form.phtml

<?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>

Additional

So here is the further information for you to fetch region based on selected country

In you JS

$(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
    });
});

In your controller

public function __construct(
        Context       $context,
        \Magento\Framework\Controller\Result\JsonFactory    $resultJsonFactory,
        \Magento\Directory\Model\RegionFactory $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()]);

    }

If you need any further help for ajax and data to be presented in phtml file please refer to this answer

Related Topic