Create dropdown attribute containing country codes, magento

Create dropdown attribute containing country codes, magento

September 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Create dropdown attribute containing country codes, magento

Extending Magento: Adding a Country Code Dropdown Attribute

As a senior developer specializing in e-commerce platforms like Magento, I often encounter requests to customize the product attributes beyond the default options provided by the platform. One common requirement is to create a dropdown attribute that displays country codes instead of full country names.

In your case, you’ve already created a dropdown attribute for countries using the catalog/product_attribute_source_countryofmanufacture source model. However, to display two-letter country codes rather than full names, we’ll need to make some adjustments to this process.

Understanding Magento’s Attribute Source Models

Magento uses source models to provide the options for dropdown attributes. The default catalog/product_attribute_source_countryofmanufacture model queries the core_country table to fetch all countries and their names. To achieve your goal, we must create a custom source model that fetches only the country codes.

Creating a Custom Country Code Source Model

To begin, let’s create a new module in Magento 2. In your project root directory, navigate to the app/code folder:

cd app/code

Next, create a new subfolder for your module with a unique vendor name and module name (e.g., YourVendor/YourModule). Inside this folder, create the following structure:

- YourVendor
  - YourModule
    - registration.php
    - etc
      - module.xml
    - Model
      - Source
        - Country.php

Implementing the Custom Country Code Source Model

In app/code/YourVendor/YourModule/Model/Source/Country.php, implement the following code:

<?php

namespace YourVendor\YourModule\Model\Source;

use Magento\Framework\Option\ArrayInterface;
use Magento\Directory\Model\CountryFactory;

class Country implements ArrayInterface
{
    protected $_countryCollection;

    public function __construct(CountryFactory $countryFactory)
    {
        $this->_countryCollection = $countryFactory->create();
    }

    public function toOptionArray()
    {
        $options = [];
        foreach ($this->_countryCollection->getItems() as $country) {
            $options[] = [
                'value' => $country->getCountryId(),
                'label' => $country->getName(),
            ];
        }
        return $options;
    }

    public function getAllOptions()
    {
        return $this->_toArray();
    }

    public function toArray()
    {
        return $this->toOptionArray();
    }
}

This model extends the built-in Magento\Directory\Model\Country class and fetches all available countries. However, it uses only the two-letter country code (getCountryId()) as the option value.

Configuring Your Custom Attribute

Now that you have a custom source model, you need to create or update your attribute configuration to use this new model.

In app/code/YourVendor/YourModule/etc/module.xml, add the following XML:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etcmodules.xsd">
    <module name="YourVendor_YourModule" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Next, in your attribute configuration file (usually located in app/code/YourVendor/YourModule/etc/attribute.xml), update the source_model field:

<attribute code="country_code" />

To change an existing attribute to use this new model, you can edit your product attributes directly in the Magento admin interface or update the database using SQLyog.

Conclusion

Creating a custom dropdown attribute for country codes in Magento involves building a custom source model and configuring it within your module. The provided example demonstrates how to create a Country class that fetches only two-letter country codes as option values.

Remember, when working with Magento 2, always ensure you follow the development best practices outlined on Laravel Company. This includes proper module organization, use of di.xml and etc/module.xml files, and adherence to version control standards.

By following these guidelines, you can create custom attributes that seamlessly integrate with the Magento platform while maintaining a clean and maintainable codebase.

Magento 2 Catalog Database Extensions

Our network