In Magento 2, extension attributes provide a way to extend the functionality of existing entities without modifying the core code. They allow you to add custom attributes to entities such as products, customers, orders, and more. Here's an example of how to create an extension attribute for products in Magento 2:1. Create a module:Create a custom module in the `app/code` directory of your Magento installation. Let's assume the module is named `Vendor_Module`.2. Define the extension attribute:Inside your module, create a `etc` directory and add a `extension_attributes.xml` file with the following content:```xml<?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\Catalog\Api\Data\ProductInterface"> <attribute code="custom_attribute" type="string"/> </extension_attributes></config>```This XML file defines a new extension attribute named `custom_attribute` of type `string` for the `ProductInterface`.3. Create a data interface:Create a new file `CustomAttributeInterface.php` in the directory `Vendor/Module/Api/Data` with the following content:```php<?phpnamespace Vendor\Module\Api\Data;interface CustomAttributeInterface{ /** * Get custom attribute * * @return string|null */ public function getCustomAttribute(); /** * Set custom attribute * * @param string $value * @return $this */ public function setCustomAttribute($value);}```This interface defines the getter and setter methods for the custom attribute.4. Implement the data interface:Create a new file `CustomAttribute.php` in the directory `Vendor/Module/Model/Data` with the following content:```php<?phpnamespace Vendor\Module\Model\Data;use Vendor\Module\Api\Data\CustomAttributeInterface;class CustomAttribute extends \Magento\Framework\Api\AbstractExtensibleObject implements CustomAttributeInterface{ /** * @inheritDoc */ public function getCustomAttribute() { return $this->_get(self::CUSTOM_ATTRIBUTE); } /** * @inheritDoc */ public function setCustomAttribute($value) { return $this->setData(self::CUSTOM_ATTRIBUTE, $value); }}```This class implements the data interface and extends the `AbstractExtensibleObject` class to provide support for extension attributes.5. Use the extension attribute:To use the extension attribute, you need to modify the code that interacts with the product entity. For example, to set and get the custom attribute:```php$product->getExtensionAttributes()->setCustomAttribute('Custom Value');$customAttribute = $product->getExtensionAttributes()->getCustomAttribute();```Remember to inject the necessary dependencies and perform the proper setup for your module. This example provides a basic outline of how to create an extension attribute for products in Magento 2. You can extend this approach to other entities as well by modifying the relevant files and configurations.