- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi
I have added a custom field (delivery_date) on checkout page.
The data is added and working when an order is saved.
I also would like to add this field in API call.
would you suggest me the way to add this attribute to 'orders' API call?
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's simple! Override API interface and add getter setter method of your custom attribute and override model function call.
Please check:
https://community.magento.com/t5/Magento-2-x-Programming/add-method-on-Login-REST-API/m-p/273579#M90...
If it helps, Please Click "Kudos" and "accept solution".
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Add a custom field to API
Hi @tvgarden
You can follow the below link.
Hope it helps!
Thanks
---
If you've found my answer useful, please give"Kudos" and "Accept as Solution".
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's simple! Override API interface and add getter setter method of your custom attribute and override model function call.
Please check:
https://community.magento.com/t5/Magento-2-x-Programming/add-method-on-Login-REST-API/m-p/273579#M90...
If it helps, Please Click "Kudos" and "accept solution".
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Add a custom field to API
Thank you, but there has been a problem.
I have added delivery_date as a date column, and added another column called delivery_data, which is text.
delivery_date is saving data, however delivery_data is not saving, also getting an error.
I followed this:
https://oyenetwork.com/articles/magento2-devliery-date-module-creation-from-scratch/
<?php namespace Vendor\Module\Plugin\Api; use Magento\Sales\Api\Data\OrderExtensionFactory; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\OrderSearchResultInterface; use Magento\Sales\Api\OrderRepositoryInterface; class OrderRepository { const DELIVERY_DATE = 'delivery_date'; const DELIVERY_DATA = 'delivery_data'; /** * Order Extension Attributes Factory * * @var OrderExtensionFactory */ protected $extensionFactory; /** * OrderRepositoryPlugin constructor * * @param OrderExtensionFactory $extensionFactory */ public function __construct(OrderExtensionFactory $extensionFactory) { $this->extensionFactory = $extensionFactory; } /** * Add "delivery_date" extension attribute to order data object to make it accessible in API data * * @param OrderRepositoryInterface $subject * @param OrderInterface $order * * @return OrderInterface */ public function afterGet(OrderRepositoryInterface $subject, OrderInterface $order) { $deliveryDate = $order->getData(self::DELIVERY_DATE); // $deliveryData = $order->getData(self::DELIVERY_DATA); $extensionAttributes = $order->getExtensionAttributes(); $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create(); $extensionAttributes->setDeliveryDate($deliveryDate); // $extensionAttributes->setDeliveryData($deliveryData); $order->setExtensionAttributes($extensionAttributes); return $order; } /** * Add "delivery_date" extension attribute to order data object to make it accessible in API data * * @param OrderRepositoryInterface $subject * @param OrderSearchResultInterface $searchResult * * @return OrderSearchResultInterface */ public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult) { $orders = $searchResult->getItems(); foreach ($orders as &$order) { $deliveryDate = $order->getData(self::DELIVERY_DATE); // $deliveryData = $order->getData(self::DELIVERY_DATA); $extensionAttributes = $order->getExtensionAttributes(); $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create(); $extensionAttributes->setDeliveryDate($deliveryDate); // $extensionAttributes->setDeliveryData($deliveryData); $order->setExtensionAttributes($extensionAttributes); } return $searchResult; } }
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Add a custom field to API
If you try to add any attribute function in interface using plugin it will not work.
for that you have to override full API call.
from webapi.xml to api model. everything you have to change because at last whole data converted to the interface object we have mentioned in route.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Add a custom field to API
@ronakchauhan427 - I've just posted a similar issue. You said, "for that you have to override full API call."
The output that I care about (for V1/products/attributes) is:
<preference for="Magento\Catalog\Api\Data\ProductAttributeInterface" type="Magento\Catalog\Model\ResourceModel\Eav\Attribute" />
as shown in:
<route url="/V1/products/attributes" method="GET">
<service class="Magento\Catalog\Api\ProductAttributeRepositoryInterface" method="getList" />
<resources>
<resource ref="Magento_Catalog::attributes_attributes" />
</resources>
</route>
I know my plugin is called for Magento\Catalog\Api\Data\ProductAttributeInterface because I can dump it in the API call and see my modification. I don't see anywhere in the file Magento\Catalog\Model\ResourceModel\Eav\Attribute that I could modify that would benefit me replacing <service class="Magento\Catalog\Api\ProductAttributeRepositoryInterface" method="getList" /> with my model.