I have question in my mind what is exact difference between Extension Attributes and Custom Attributes ?
Any one here who can provide me some ideas ?!
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.Sign up to join this community
Custom attributes are those added on behalf of a merchant. For example, a merchant might need to add custom attributes to product entity named ‘customizeable’ in which he want to save information that this product will be customized from 3rd party or not .
Both custom and extension attributes have a bit extent same functionality (to keep data) but there are considerable difference between custom attributes & extension attributes functionality. e.g.
we can add extra search criteria with extension attribute by joining it with any other DB table. We can authenticate it by adding ACL in it. Lets explain in little detail.
Please have a look at the following snippet. You must create a /etc/extension_attributes.xml
<config> <extension_attributes for="Path\To\Interface"> <attribute code="name_of_attribute" type="datatype"> <resources> <resource ref="permission"/> </resources> <join reference_table="" reference_field="" join_on_field=""> <field>field1</field> <field>field2</field> </join> </attribute> </extension_attributes> </config>
Searching of extension attributes: In join tag you can define your concerning table with which you want to join and get data from this table. In reference_table attribute you have to give table name while reference_field will be the primary key (first table key for which this extension attribute create like in this example products)on which you put your relation/join. join_on_field as name suggesting will be the second tables PK against which we want records. Under fields tags you can add all your required fields actually you want to get data
Extension attribute authentication: You can restrict this particular extension attribute value by using tag To understand this Lets have an example. Say we have more then one extension attributes for products and one of them say extra_special_price we don't want to show it to all users. We can restrict this attribute. Have a look at the following snippet.
<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="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface"> <resources> <resource ref="Magento_CatalogInventory::cataloginventory"/> </resources> </attribute> </extension_attributes> <attribute code="logo_size" type="string"/> </config>
In this example, the stock_item attribute is restricted to only the users who have the Magento_CatalogInventory::cataloginventory permission while logo_size dont have any condition on it.
So now if you get product object (in what ever way via api or from web) if current user have permissions for cataloginventory he will get stock_items values only.
Out of scope of this question but have a look how you will create the attribute using custom_extension in the following link in very simple way: How to create extension_attributes
Custom attributes are the attributes added to describe an entity, such as product attributes, customer attributes etc. These are a subset of EAV attributes.
Extension attributes on the other hand are generally used for more complex data types such as adding additional complex data into an entity from a custom external table.
Simply put, custom attributes conform to EAV standards whereas extension attributes are used for more complex data which custom attributes cannot handle.