How To

How to Show Abbreviations of State/Province Instead of Full Names in Magento 2?

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!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Organic Traffic Drop: Sudden Decline Causes and Solutions

A sudden drop in organic traffic can be alarming for any business. Organic traffic is…

10 hours ago

Difference Between: Magento 2 WhatsApp Order Notification vs. Magento 2 SMS Notification

In the fast-paced world of eCommerce, effective communication is key to maintaining customer satisfaction and…

10 hours ago

Understanding useSearchParams vs useParams Hooks in Remix

In the Remix framework, handling URL parameters is a common task when building dynamic web…

1 day ago

How to Implement Frontend Design Customization in Shopify Remix App?

In this blog post, we'll show you how to implement frontend design customization in the…

3 days ago

Your Ultimate Guide to Shopify Headless Commerce

Starting an eCommerce business with Shopify can be easy yet challenging in many different ways,…

5 days ago

Magento 2: How to Show Custom Notice Message Before Payment Step on Checkout

Hello Magento Friends, In Magento 2, you can customize the checkout process to enhance the…

5 days ago