Service Contracts

Magento is a modular system that enables third-party developers to customize and overwrite 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?

 

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

app/code/Dckap/ExtensionAttributes/registration.php

2. Create the install script in the Setup directory.

app/code/Dckap/ExtensionAttributes/Setup/InstallSchema.php

3. Define the extension attribute in XML

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

 

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

 

Planning to implement Magento 2

Our Magento-certified experts come up with best-in-class solutions to improve conversions, increase sales and also provide a great user experience.

Get In Touch

 

5. Assigning the value for  extension attributes in knockout  js

app/code/Dckap/ExtensionAttributes/view/frontend/web/js/view/shipping.js

Observe the input field in initialize function

Set the value to extension attributes in shipping information

 

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

7. Retrieve the extension attributes value using observer

Define the observer in events.xml
app/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

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

References

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Ready to drive online revenue

Get In Touch

2 thoughts on “Extension Attributes in Magento2

Leave a Reply

Your email address will not be published. Required fields are marked *