Skip to main content
Magento

Extension Attributes in Magento2

By |December 14, 2018 March 7th, 2022No Comments

Service Contracts

Magento is a modular system that enables third-party developers to customize and overwrite the core parts of its framework. This flexibility, however, comes at a price. Business logic tends to leak across the layers of the Magento system, which manifests as a duplicated and inconsistent code.

Merchants might be reluctant to upgrade Magento because customized extensions that they have purchased might not be compatible with new versions of Magento. Also, Magento and third-party developers can find it difficult to track and report the dependencies that customized extensions have on other extensions.

To address these issues, the Magento system introduces service contracts.

Extension Attributes

Extension attributes are new in Magento 2. They are used to extend functionalities and often use more complex data types than custom attributes. Extension Attributes are used to allow for customization of the strict Service Contracts. These attributes do not appear on the GUI.

How to define Extension Attributes?

<config>
 <extension_attributes for="Path\To\Interface">
 <attribute code="name_of_attribute" type="datatype">
 </attribute>
 </extension_attributes>
</config>


How to use extension attributes in Magento 2 Checkout?

1. Create a module.xml and registration.php to register your module.

For example, app/code/Dckap/ExtensionAttributes /etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Dckap_ExtensionAttributes" setup_version="1.0.0" />
</config>

app/code/Dckap/ExtensionAttributes/registration.php

2. Create the install script in the Setup directory.app/code/Dckap/ExtensionAttributes/Setup/InstallSchema.php

startSetup();

/* While module install, creates columns in quote_address and sales_order_address table */

$eavTable1 = $installer->getTable('quote_address');
$eavTable2 = $installer->getTable('sales_order_address');

$columns = [
'sample_attribute' => [
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'nullable' => false,
'comment' => 'freightcollect',
] 
];

$connection = $installer->getConnection();
foreach ($columns as $name => $definition) {
$connection->addColumn($eavTable1, $name, $definition);
$connection->addColumn($eavTable2, $name, $definition);
}

$installer->endSetup();
}
}

3. Define the extension attribute in XMLapp/code/Dckap/ExtensionAttributes/etc/extension_attributes.xml






4. Create the field for extension attributes in knockout HTMLapp/code/Dckap/ExtensionAttributes/view/frontend/web/template/shipping-address/shipping-method-item.html


<input type="text" name="freight_collect_account_number" id="freight_collect_account_number" data-bind="value: sample_attribute, valueUpdate: 'afterkeydown', event: { 'keyup': check }">

Migrate To Magento 2 5. Assigning the value for  extension attributes in knockout  jsapp/code/Dckap/ExtensionAttributes/view/frontend/web/js/view/shipping.jsObserve the input field in initialize function

initialize: function (config) {
this.sample_attribute = ko.observable();
}

Set the value to extension attributes in

shipping information

setShippingInformation: function () {
var sample_attribute_value = $("#freight_collect_account_number").val();
if (quote.shippingAddress().extensionAttributes == undefined) {
quote.shippingAddress().extensionAttributes = {};
}
quote.shippingAddress().extensionAttributes.sample_attribute=sample_attribute_value;
};

6. Save the extension attribute value using the plugin

Define the plugin in di.xml

app/code/Dckap/ExtensionAttributes/etc/di.xml







Save the value of extension attributes in

qoute address

app/code/Dckap/ExtensionAttributes/Plugin/Quote/FreightCollect.php

getExtensionAttributes()){ $subject->setSampleAttribute($subject->getExtensionAttributes()->getSampleAttribute());
}
return $result;
}
}

7. Retrieve the extension attributes value using observerDefine the observer in events.xmlapp/code/Dckap/ExtensionAttributes/etc/events.xml








Get the value of extension attributes and save in order using the observer
app/code/Dckap/ExtensionAttributes/Observer/ Observeorder.php

getEvent()->getOrder();
$quote = $observer->getEvent()->getQuote(); $order->getShippingAddress()->setSampleAttribute($quote->getShippingAddress()->getSampleAttribute());
return $this;
}
}

I hope the article was useful. Please let us know if you have any queries or comments.

References

Magento development services include extensions which can be customized according to the store owner needs. Learn about extension attributes in Magento 2

Sridevi

Author Sridevi

Sridevi Murugaraj, a junior software engineer, started her career in Magento eCommerce platform. Within months she gained extensive knowledge and now she is exemplar of developers building Magento solutions and Android applications. She craves to cook her own recipes and rejoices in listening to world music.

More posts by Sridevi

Discover What You’re Missing

Get the weekly email full of actionable ideas and insights you can use at work and home.