19

Is there anyone that can give me an example on how to use extension_attributes in Magento2?

Phoenix128_RiccardoT
  • 7,065
  • 2
  • 22
  • 36

1 Answers1

25

Suppose we create an order delivery date, when the customer select shipping then the customer can select a delivery date. So you can create a extension attribute for \Magento\Checkout\Api\Data\ShippingInformationInterface in following way


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="delivery_date" type="string"/>
    </extension_attributes>
</config>

So now you can access this value when you using \Magento\Checkout\Api\Data\ShippingInformationInterface as di.

So in that case Magento\Checkout\Model\ShippingInformationManagement this class use \Magento\Checkout\Api\Data\ShippingInformationInterface as a params for saveAddressInformation method. So you can access following way:


$extAttributes = $addressInformation->getExtensionAttributes();
$deliveryDate = $extAttributes->getDeliveryDate();

For more details(Magento 2 Official Documentation)

Here is an example:

extension_attributes

Pick Extension Attributes value

[Update]

You can set extension_attributes by setExtensionAttributes method. Following class is an example: Click Here

Sohel Rana
  • 35,846
  • 3
  • 72
  • 90
  • So what is setExtensionAttributes for? – Phoenix128_RiccardoT May 01 '16 at 16:13
  • I updated with real example – Sohel Rana May 01 '16 at 18:20
  • Great, but one thing is missing here: Where exactly are extension attributes stored? Do I have to provide code for saving and retrieving attributes myself or can Magento handle that (like with custom attributes)? – Martin Wickman Jun 01 '16 at 14:59
  • Do I understand it correctly that plugins are mandatory in order for extension_attributes to work? For example, do I always have to add a plugin for afterLoad for customers if I want to have a custom attribute for customers? Seems like a bit overhead to me ... – Giel Berkers Nov 16 '16 at 14:28
  • you can also add join directives to the extension attributes if the value comes from another table. Although I did not find a solution yet to save/load them in/from the same table without using a plugin – David Verholen Feb 22 '17 at 17:35
  • 1
    Thanks @SohelRana, It's working fine in default Magento setup, but when I install OneStepCheckout module then i can't get value in the post, I mean in quote object so what should i need to change for that.... I get this type value in quote object

    [delivery_date] => 0000-00-00 00:00:00

    ?

    – Dhrumin Nov 17 '17 at 09:45
  • Hi @SohelRana can you please help me on this https://magento.stackexchange.com/questions/219230/magento-2-how-to-add-extension-attribute-value-to-response – Nagaraju Kasa Mar 22 '18 at 10:00
  • I have created extension attribute and i am able to save extension attribute to billing and shipping address but i need to show when use rest/V1/guest-carts/:cartId/billing-address and rest/V1/mine/:cartId/billing-address these APIs – Nagaraju Kasa Mar 22 '18 at 10:02
  • 1
    @MartinWickman yes it is the responsibility of the developer to implement logic for retrieving and persisting of extension attribute data. The concept is, you have some possibly complex data introduced by your Module for Product Entities and this data might come from one or more database tables, or some other data source, it should not matter. Using the Plugin system, target the ProductRepository's methods for loading (e.g., afterGet, afterGetList). Here, you retrieve your data (from wherever) and add it to the Entity's ExtensionAttributes. This seems arduous but is very flexible and powerful. – John Hall Feb 11 '19 at 20:07
  • Why we use extension attributes ? – Hamendra Sunthwal Mar 23 '21 at 05:12