Managing an e-commerce platform involves various tweaks to improve user experience and streamline operations. One such customization in Magento 2 is displaying state or province abbreviations instead of their full names. This can be particularly useful for shipping addresses, invoices, and other areas where space is limited, or where abbreviations are preferred for consistency.
In this blog, we will walk you through the steps to achieve this customization in Magento 2.
Steps to Show Abbreviations of State/Province Instead of Full Names in Magento 2:
Step 1: Create the di.xml file inside the Extension etc folder.
app\code\Vendor\Extension\etc\
Then add the code as follows
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Directory\Helper\Data" type="Vendor\Extension\Helper\CustomData" /> </config>
Step 2: Create the CustomData.php file inside the Extension Helper folder.
app\code\Vendor\Extension\Helper\
Now add the below-mentioned code
<?php namespace Vendor\Extension\Helper; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\ScopeInterface; use Magento\Directory\Model\AllowedCountries; class CustomData extends \Magento\Directory\Helper\Data { private const STORE_ID = 'store_id'; /** * Retrieve current Product object * * @return \Magento\Catalog\Model\Product|null */ public function getRegionData() { $scope = $this->getCurrentScope(); $allowedCountries = $this->scopeConfig->getValue( AllowedCountries::ALLOWED_COUNTRIES_PATH, $scope['type'], $scope['value'] ) ?? ''; $countryIds = explode(',', $allowedCountries); $collection = $this->_regCollectionFactory->create(); $collection->addCountryFilter($countryIds)->load(); $regions = [ 'config' => [ 'show_all_regions' => $this->isShowNonRequiredState(), 'regions_required' => $this->getCountriesWithStatesRequired(), ], ]; foreach ($collection as $region) { /** @var $region \Magento\Directory\Model\Region */ if (!$region->getRegionId()) { continue; } $regions[$region->getCountryId()][$region->getRegionId()] = [ 'code' => $region->getCode(), 'name' => (string)__($region->getCode()), ]; } return $regions; } private function getCurrentScope(): array { $scope = [ 'type' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 'value' => null, ]; $request = $this->_getRequest(); if ($request->getParam(ScopeInterface::SCOPE_WEBSITE)) { $scope = [ 'type' => ScopeInterface::SCOPE_WEBSITE, 'value' => $request->getParam(ScopeInterface::SCOPE_WEBSITE), ]; } elseif ($request->getParam(ScopeInterface::SCOPE_STORE)) { $scope = [ 'type' => ScopeInterface::SCOPE_STORE, 'value' => $request->getParam(ScopeInterface::SCOPE_STORE), ]; } elseif ($request->getParam(self::STORE_ID)) { $scope = [ 'type' => ScopeInterface::SCOPE_STORE, 'value' => $request->getParam(self::STORE_ID), ]; } else { $storeId = $this->_storeManager->getStore()->getId() ?? null; if ($storeId) { $scope = [ 'type' => ScopeInterface::SCOPE_STORE, 'value' => $storeId, ]; } } return $scope; } }
Output:
Conclusion:
Customizing Magento 2 to display state or province abbreviations can significantly improve user experience and maintain consistency across your store. For any other customization requirement, get in touch with experienced Magento Developers.
If you have any questions or run into issues, feel free to leave a comment below.
Happy Coding!