In order to show the region fields should be shown for non required state :
(can be configured from Stores / Configuration / General / State Options / Allow to Choose State if it is Optional for Country)
vendor/magento/module-directory/Helper/Data.php:268
/*
* Path to config value, which detects whether or not display the state for the country, if it is not required
*/
const XML_PATH_DISPLAY_ALL_STATES = 'general/region/display_all';
/**
* Return, whether non-required state should be shown
*
* @return bool
*/
public function isShowNonRequiredState()
{
return (bool)$this->scopeConfig->getValue(
self::XML_PATH_DISPLAY_ALL_STATES,
ScopeInterface::SCOPE_STORE
);
}
In order to set the region required for a country :
(can be configured from Stores / Configuration / General / State Options / State is Required for)
vendor/magento/module-directory/Helper/Data.php:281
/*
* Path to config value, which lists countries, for which state is required.
*/
const XML_PATH_STATES_REQUIRED = 'general/region/state_required';
/**
* Returns flag, which indicates whether region is required for specified country
*
* @param string $countryId
* @return bool
*/
public function isRegionRequired($countryId)
{
$countyList = $this->getCountriesWithStatesRequired();
if (!is_array($countyList)) {
return false;
}
return in_array($countryId, $countyList);
}
Then to add a new country and region :
- Add a now country inside this table :
directory_country
- Add related region inside this table :
directory_country_region
related with the country_id column.
- Add translated version of your region in this table :
directory_country_region_name
Add these information from a setup script. You can based on the Magento Directory Setup Data : vendor/magento/module-directory/Setup/InstallData.php:17
To add the dropdowns in your custom module for checkout. You can use /vendor/magento/module-checkout/Block/Checkout/DirectoryDataProcessor.php
as reference.
You need to create a plugin for the \Magento\Checkout\Block\Checkout\DirectoryDataProcessor::process
method. In your plugin, implement the after method adding the option data to the corresponding layout nodes at run-time.
Create di.xml to declare the plugin
<?xml version='1.0'?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd" >
<type name="Magento\Checkout\Block\Checkout\DirectoryDataProcessor">
<plugin name='customDropdownPlugin' type='VendorName\ModuleName\Plugin\Block\Checkout\DirectoryDataProcessorPlugin' sortOrder='999'/>
</type>
</config>
Create VendorName\ModuleName\Plugin\Block\Checkout\DirectoryDataProcessorPlugin.php
<?php
namespace VendorName\ModuleName\Plugin\Block\Checkout;
class DirectoryDataProcessorPlugin
{
/**
* @param \Magento\Checkout\Block\Checkout\DirectoryDataProcessor $subject
* @param array $jsLayout
* @return array
*/
public function afterProcess(
\Magento\Checkout\Block\Checkout\DirectoryDataProcessor $subject,
array $jsLayout
) {
if (isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
$jsLayout['components']['checkoutProvider']['dictionaries'] = [
'custom_country_id' => $subject->getCountryOptions(),
'custom_region_id' => $subject->getRegionOptions(),
];
}
return $jsLayout;
}
}
Here you need to replace custom_cuntry_id
and custom_region_id
with your fileds respectively.
Also check your attribute you have created must be of type select in eav_attribute
table for customer address entity.
Best Answer
Include quote js like
in custom js. And try
it will give you country ID