Knowing About Magento 2 Extension Attributes in Detail
In order to bring in more stability to Magento 2, it integrated Service Contracts. On the other hand, Magento 2 Extension Attributes are utilized to enable customization of service contracts. This article will help you know more about extensions attributes in Magento 2
What are Service contracts?
Service contracts form the prime elements of Magento 2. They are basically sets of php interfaces defining some elements of a particular module.
With the creation of interfaces the contracts are defined that are categorized, implemented and followed. When you have a transparent contract it becomes easy to understand what is expected from a specific class and reach solid executions. There are plenty of benefits associated with the Service Contract system
Simple to shift between solid implementation (it can be done by setting up a preference for a particular interface in dependency injection configuration files etc/di.xml
- Clear public API of the classes
- Easier unit testing
- Consistency over versions
Add new properties in M1
In M1 adding information to quote, invoice, order, etc was the basic requirement. The simplest method to attain that was to include fields to coinciding database tables such as -sales_flat_quote and sales_flat_order and then fill it through fieldsets
It transforms a quote field to order. This attribute then becomes the section of order class’s $_data property which can be accessed easily with $order->getYourSpecialAttribute().
M2 point of view
As stated earlier, Magento 2 works with interface method as there will be no immediate accessor methods present which were put to use in M1. Here is the Service Contract for - Sales Order class
Yes, you will observe a plenty of getters & setters in the form of entities which are considered as data transfer items.
New attribute addition
It is not possible to edit the interface often when you wish to include a new attribute as that would affect the backward compatibility. The whole idea is to remain in coordination with service contracts.
At this stage we need to bypass the interface because most classes continue to still extend \Magento\Catalog\Model\AbstractModel but don’t carry out over one reason.
- In case you set a different preference for this particular service contract then your code will break
- The change remains undocumented as it is based on magic
Then what is the best method to add new attributes in a modest manner that works well with the interfaces’ limitations.
Understanding Extension attributes
Just observe the other end of your interface and here are the two methods
Extension attributes are Magento 2’s aspects to offer increased stability and customisability too.
Types of attributes and features
Custom and Entity-Attribute-Value (EAV) attributes
Custom attributes are the ones added on the merchant’s behalf. The merchant can easily add the attributes using the Admin panel in Magento.
Custom attributes are part of EAV attributes
Objects that utilize EAV attributes basically store values in different MySQL tables. The Customer & Catalog modules are known as the primary models that utilize EAV attributes. Other modules, like GiftMessage, ConfigurableProduct, and Tax, make use of EAV functionality for Catalog.
Extension attributes are basically newly introduced elements in Magento 2. They are utilized to extend functionality as well they make use of a complex set of data types rather than the custom attributes. You will not find these attributes in the Magento Admin.
Certainly, the efforts involved for extension attributes in Magento 2 are more than in Magento, then, you may ask why should you go for it?
The changes that come with extension attributes on service contracts are far ahead in terms of stability of the platform while attaining extensibility. Also, there are a number of benefits you can reap in using extension attributes. The benefits include the automatic availability of attribute data added through API requests.