Creating extension attributes in Magento 2

About Extension Attribute

Using Extension attribute we can extends the data interface functionality without modify the actual file. We can add additional fields to Data interface as per our needs.

We can create extension attributes using extension_attributes.xml file. In that file need to pass the data interface and your attribute name that you need to create.

It’s not require to create that field in database. We can create extension attribute without the database field or attribute.

Let’s create one small module for extension attributes.

Register extension attribute module in Magento 2 Application.

Using registration.php file we can register our module component to Magento 2 application.

<?php
//file path: MAGENTO_ROOT/app/code/RS/ExtAttributes/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'RS_ExtAttributes',
    __DIR__
);

After register the our module to Magento, Need to config module version and dependency.

Config module dependency and configure module version.

Using module.xml we can configure module version and module dependency.

<?xml version="1.0" ?>
<!-- file path: MAGENTO_ROOT/app/code/RS/ExtAttributes/etc/module.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
	<module name="RS_ExtAttributes" setup_version="1.0.0">
	</module>
</config>

Create extension attribute

Using extension_attributes.xml file we can create extension attributes.

<?xml version="1.0" ?>
<!-- file path: MAGENTO_ROOT/app/code/RS/ExtAttributes/etc/extension_attributes.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
        <attribute code="custom_note" type="string" />
    </extension_attributes>
</config>

In above example I did not create any attribute or database field for that extension attribute. We can create extension attribute without any attribute or any database field.

Validate extension attribute is created or not

For validating extension attribute, We need to install this module.

php bin/magento module:enable RS_ExtAttributes
php bin/magento setup:upgrade
php bin/magento setup:di:compile

After that just open that Product extension class from the generated derectory.

just check that two

<magento_root>/generated/code/Magento/Catalog/Api/Data/ProductExtension.php

<magento_root>/generated/code/Magento/Catalog/Api/Data/ProductExtensionInterface.php

It’s auto generated extension attribute methods to that both class.

Let me add the screen sorts.

Get extension attribute data

At the moment creating the custom script to get extension attribute and it’s data

<?php

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\State;
use Magento\Catalog\Api\ProductRepositoryInterface;

require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get(State::class);
$state->setAreaCode('frontend');

$productRepository = $objectManager->create(ProductRepositoryInterface::class);
$product = $productRepository->getById(1);
$extentionAttr = $product->getExtensionAttributes();

print_r(get_class_methods($extentionAttr));
print_r($extentionAttr->getCustomNote()); /* return null due to that we need to set menually when product data is load */

That’s all for creating extensions attribute in Magento 2.