Embark on a journey of refined customization in Magento development, a strategic imperative for tailoring solutions to meet your unique business requirements. Our team of seasoned Magento 2 experts, available for hire, is ready to guide you through seamless integration, ensuring an optimal balance between flexibility and platform integrity. As a reputable Magento Development Company, we specialize in Magento for eCommerce development and Magento website development.
There are two types of Attributes:
- Scalar Attributes: Attributes that can only contain scalar values, that is, boolean, int, float, or string values.
- Non-scalar Attributes: Attributes that can use objects to have more complex types. You will require to create a custom interface for these attributes.
Extension Attribute Configuration file :
/vendor/module-quote/etc/extension_attributes.xml
The extension_attribute.xml file is allowed to declare the extension attribute code and the type of that entity we want to add for them.
Basically, the ExtensibleDataInterface interface is used as a parent interface for an entity interface.
The \Magento\Catalog\Api\Data\ProductInterface interface provides setExtensionAttributes() and getExtensionAttributes() methods in addition to all other getter and setter methods that help to operate with customer-related data.
So We need at least two plugins to use the getExtensionAttributes() and setExtensionAttributes().
- Add Plugin To Product Repository to Get Extension Attribute
We are creating a plugin in di.xml.
- Add Plugin To Product Repository to Set Extension Attribute
_helperData = $helperData;
$this->ourRepositoryInterface = $ourRepositoryInterface;
$this->request = $request;
}
public function afterSave
(
ProductRepositoryInterface $subject,
ProductInterface $result, /** result from the save call **/
ProductInterface $entity /** original parameter to the call **/
/** other parameter not required **/
) {
$extensionAttributes = $entity->getExtensionAttributes(); /** get original extension attributes from entity **/
$ourCustomData = $extensionAttributes->getAdditionalPrice();
$this->ourRepositoryInterface->save($ourCustomData);
$resultAttributes = $result->getExtensionAttributes(); /** get extension attributes as they exist after save **/
$resultAttributes->setAdditionalPrice($ourCustomData); /** update the extension attributes with correct data **/
$result->setExtensionAttributes($resultAttributes);
return $result;
}
}
Note : We have only use the getAfter and saveAfter you can also pass the extention attribute data in the getList call.
The Result looks likes as:
{
"sku": "Jeans",
"price": "4220.00",
"description": "New design",
"extension_attributes": {
"additional_price": 10
}
}
Unlock the full potential of extension attributes to enhance your online store’s functionalities, deliver unparalleled user experiences, and achieve your business objectives with finesse. Collaborate with our team and let’s elevate your Magento 2 development to new heights.