So basically I want to extend the existing web-service /V1/orders/:id for adding new order field (say: erp_id).
It's a field in sales_order table and it's available when you load the $order object.
My main concern here is not to use the extension_attributes node for adding this new field. I want it to be on the same level as other standard fields.
The reason behind this is we already have many third parties relying on this structure.
So far gathered the key files for extension seems to be:
Magento\Sales\Api\OrderRepositoryInterface(service interface for the API)Magento\Sales\Api\Data\OrderInterface(data interface for the API result)Magento\Sales\Model\OrderRepository(concrete class for service interface)Magento\Sales\Model\Order(concret class for data interface - esp. getters and setters)
Some similar queries waiting for an answer:
== EDIT ==
If you edit as below, you can have the custom field on the top-level in the SOAP response.
File: app/code/Magento/Sales/Api/Data/OrderInterface.php
//...
/*
* ERP ID
*/
const ERP_ID = 'erp_id';
//...
/**
* Gets the ERP Id
*
* @return int
*/
public function getErpId();
/**
* Sets the ERP Id
*
* @param int $id
* @return $this
*/
public function setErpId($id);
File: app/code/Magento/Sales/Model/Order.php
/**
* {@inheritdoc}
*/
public function getErpId()
{
return $this->getData(OrderInterface::ERP_ID);
}
/**
* {@inheritdoc}
*/
public function setErpId($id)
{
return $this->setData(OrderInterface::ERP_ID, $id);
}
It's that simple logically. But the problem here is overriding the interface.
Another Question:
is this possible in Magento 2 <preference for="Magento\Sales\Api\Data\OrderInterface" type="My\Custom\Api\Data\OrderInterface" />
(i.e. overriding of one interface by another)
-
Did you get the solnMahendra Jella– Mahendra Jella2017年11月14日 09:50:57 +00:00Commented Nov 14, 2017 at 9:50
-
Nope. Ended up using extension_attributes.MagePsycho– MagePsycho2017年11月14日 10:33:21 +00:00Commented Nov 14, 2017 at 10:33
-
can you share your code pls. I tried but it didn't work for meMahendra Jella– Mahendra Jella2017年11月14日 13:24:19 +00:00Commented Nov 14, 2017 at 13:24
-
Same approach posted by Ryan HoerrMagePsycho– MagePsycho2017年11月15日 07:04:03 +00:00Commented Nov 15, 2017 at 7:04
1 Answer 1
The intended mechanism by which to add attributes to existing models is via the extension_attributes mechanism. It take some work to set up, but this exposes the data consistently throughout the API (both internally, for code hinting, etc., and externally, for SOAP and REST).
To my knowledge, there is no mechanism for adding top-level attributes to the API, nor is there supposed to be.
Rewriting a variety of core classes to accomplish this would be highly invasive and likely to conflict with other plugins trying to do the same. It would also be a complete dismissal of Magento 2's conventions and best practices.
There are a variety of resources on working with Extension Attributes:
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html
How do the extension attributes work in Magento 2?
How to use extension_attributes in Magento2
-
1As I said I needed this due to the nature of the project. I am not sure if we can override service/data interfaces. But I needed to add top-level attributes without editing the core.MagePsycho– MagePsycho2016年09月22日 20:57:48 +00:00Commented Sep 22, 2016 at 20:57
Explore related questions
See similar questions with these tags.