Adding extension attributes to Order API Endpoint
EDIT: updated with more code.
I'm looking to add some more data to
/rest/VI/orders/{orderID}
through the use of extension attributes.
I have read through various ways of doing this, but they seem to have either conflicting information, or simply don't work -
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html
https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
How is it possible to set an extension_attributes to the orders and have it appear with REST/SOAP API?
I would need to take in extra information using the REST API and then have this displayed at the endpoint outlined above. The values would also need to be stored in the database, and i'm guessing retrieved from here for the endpoint.There would be nothing on the frontend for the customer to fill in - this is purely outside of Magento using the REST API to create orders.
Simply adding the extension_attributes file, should the custom_part I added be viewable at the endpoint - I understand it would be blank if so.
For setting and getting data, should the type=""
point to a file such as vendormoduleApiDataAttributeInterface
If so, what should the contents of this file be? Would I need to use a plugin to do this instead? How are these extension attributed added to the database?
Any help would this would be appreciated.
EDIT:
I have got it to be added to the endpoint on the local Swagger page. However, when i try to call the endpoint with Postman it gives a 500 error.
I have tried looking at the Magento/GiftMessage module and copied / changed some of the code to try and replicate what was happening.
My files now look like so -
di.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MlCustomDirectDebitApiOrderRepositoryInterface" type="MlCustomDirectDebitModelOrderRepository"/>
<type name="MagentoSalesApiOrderRepositoryInterface">
<plugin name="save_sort_code" type="MlCustomDirectDebitModelPluginOrderSave"/>
<plugin name="get_sort_code" type="MlCustomDirectDebitModelPluginOrderGet"/>
</type>
</config>
extension_attributes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoSalesApiDataOrderInterface">
<attribute code="sort_code" type="MlCustomDirectDebitApiDataMessageInterface" />
</extension_attributes>
</config>
Api/Data/MessageInterface.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitApiData;
/**
* Interface MessageInterface
* @api
*/
interface MessageInterface extends MagentoFrameworkApiExtensibleDataInterface
{
/**#@+
* Constants for keys of data array. Identical to the name of the getter in snake case
*/
// const MESSAGE = 'message';
const SORT_CODE ='sort_code';
/**
* Return the sort code.
*
* @return int|null sort code. Otherwise, null.
*/
public function getSortCode();
/**
* Set the sort code.
*
* @param string $sortCode
* @return $this
*/
public function setSortCode($sortCode);
/**
* Retrieve existing extension attributes object or create a new one.
*
* @return MlCustomDirectDebitApiDataMessageExtensionInterface|null
*/
public function getExtensionAttributes();
/**
* Set an extension attributes object.
*
* @param MlCustomDirectDebitApiDataMessageExtensionInterface $extensionAttributes
* @return $this
*/
public function setExtensionAttributes(
MlCustomDirectDebitApiDataMessageExtensionInterface $extensionAttributes
);
}
Api/OrderRepositoryInterface.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitApi;
/**
* Interface OrderRepositoryInterface
* @api
*/
interface OrderRepositoryInterface
{
/**
* Return the sort code for a specified order.
*
* @param int $orderId The order ID.
* @return MlCustomDirectDebitApiDataMessageInterface Sort code.
* @throws MagentoFrameworkExceptionNoSuchEntityException
*/
public function get($orderId);
/**
* Set the sort code for an entire order.
*
* @param int $orderId The order ID.
* @param MlCustomDirectDebitApiDataMessageInterface $sortCode The sort code.
* @return bool
* @throws MagentoFrameworkExceptionNoSuchEntityException
* @throws MagentoFrameworkExceptionInputException
* @throws MagentoFrameworkExceptionCouldNotSaveException
* @throws MagentoFrameworkExceptionStateInvalidTransitionException
*/
public function save($orderId, MlCustomDirectDebitApiDataMessageInterface $sortCode);
}
Model/Plugin/OrderGet.php
<?php
/**
*
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitModelPlugin;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderGet
{
/** @var MlCustomDirectDebitApiOrderRepositoryInterface */
protected $sortCodeOrderRepository;
/** @var MagentoSalesApiDataOrderExtensionFactory */
protected $orderExtensionFactory;
/**
*
* @Todo
* Okay, so go through this and the other file and remove any instance of "message" and change it to "sortCode"
* Make sure all the files actually exist where they need to
*/
/**
* Init plugin
*
* @param MlCustomDirectDebitApiOrderRepositoryInterface $sortcodeOrderRepository
* @param MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
*/
public function __construct(
MlCustomDirectDebitApiOrderRepositoryInterface $sortCodeOrderRepository,
MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
) {
$this->sortcodeOrderRepository = $sortCodeOrderRepository;
$this->orderExtensionFactory = $orderExtensionFactory;
}
/**
* Get sort code
*
* @param MagentoSalesApiOrderRepositoryInterface $subject
* @param MagentoSalesApiDataOrderInterface $resultOrder
* @return MagentoSalesApiDataOrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
MagentoSalesApiOrderRepositoryInterface $subject,
MagentoSalesApiDataOrderInterface $resultOrder
) {
$resultOrder = $this->getOrderSortcode($resultOrder);
return $resultOrder;
}
/**
* Get sort code for order
*
* @param MagentoSalesApiDataOrderInterface $order
* @return MagentoSalesApiDataOrderInterface
*/
protected function getOrderSortCode(MagentoSalesApiDataOrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getSortCode()) {
return $order;
}
try {
/** @var MlCustomDirectDebitApiDataMessageInterface $sortCode */
$sortCode = $this->sortCodeOrderRepository->get($order->getEntityId());
} catch (NoSuchEntityException $e) {
return $order;
}
/** @var MagentoSalesApiDataOrderExtension $orderExtension */
$orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
$orderExtension->setsortCode($sortCode);
$order->setExtensionAttributes($orderExtension);
return $order;
}
}
Model/Plugin/OrderSave.php
<?php
/**
*
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitModelPlugin;
use MagentoFrameworkExceptionCouldNotSaveException;
class OrderSave
{
/** @var MlCustomDirectDebitApiOrderRepositoryInterface */
protected $sortCodeOrderRepository;
/**
* Init plugin
*
* @param MlCustomDirectDebitApiOrderRepositoryInterface $sortCodeOrderRepository
*/
public function __construct(
MlCustomDirectDebitApiOrderRepositoryInterface $sortcodeOrderRepository
) {
$this->sortCodeOrderRepository = $sortcodeOrderRepository;
}
/**
* Save sort code
*
* @param MagentoSalesApiOrderRepositoryInterface $subject
* @param MagentoSalesApiDataOrderInterface $resultOrder
* @return MagentoSalesApiDataOrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws CouldNotSaveException
*/
public function afterSave(
MagentoSalesApiOrderRepositoryInterface $subject,
MagentoSalesApiDataOrderInterface $resultOrder
) {
/** @var MlCustomDirectDebitApiDataOrderInterface $resultOrder */
$resultOrder = $this->saveOrderSortcode($resultOrder);
return $resultOrder;
}
/**
* Save sort code for order
*
* @param MagentoSalesApiDataOrderInterface $order
* @return MagentoSalesApiDataOrderInterface
* @throws CouldNotSaveException
*/
protected function saveOrderSortCode(MagentoSalesApiDataOrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if (
null !== $extensionAttributes &&
null !== $extensionAttributes->getSortCode()
) {
/* @var MlCustomDirectDebitApiDataMessageInterface $sortCode */
$sortCode = $extensionAttributes->getSortCode();
try {
$this->sortCodeOrderRepository->save($order->getEntityId(), $sortCode);
} catch (Exception $e) {
throw new CouldNotSaveException(
__('Could not add sort code to order: "%1"', $e->getMessage()),
$e
);
}
}
return $order;
}
}
magento2 rest-api
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
EDIT: updated with more code.
I'm looking to add some more data to
/rest/VI/orders/{orderID}
through the use of extension attributes.
I have read through various ways of doing this, but they seem to have either conflicting information, or simply don't work -
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html
https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
How is it possible to set an extension_attributes to the orders and have it appear with REST/SOAP API?
I would need to take in extra information using the REST API and then have this displayed at the endpoint outlined above. The values would also need to be stored in the database, and i'm guessing retrieved from here for the endpoint.There would be nothing on the frontend for the customer to fill in - this is purely outside of Magento using the REST API to create orders.
Simply adding the extension_attributes file, should the custom_part I added be viewable at the endpoint - I understand it would be blank if so.
For setting and getting data, should the type=""
point to a file such as vendormoduleApiDataAttributeInterface
If so, what should the contents of this file be? Would I need to use a plugin to do this instead? How are these extension attributed added to the database?
Any help would this would be appreciated.
EDIT:
I have got it to be added to the endpoint on the local Swagger page. However, when i try to call the endpoint with Postman it gives a 500 error.
I have tried looking at the Magento/GiftMessage module and copied / changed some of the code to try and replicate what was happening.
My files now look like so -
di.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MlCustomDirectDebitApiOrderRepositoryInterface" type="MlCustomDirectDebitModelOrderRepository"/>
<type name="MagentoSalesApiOrderRepositoryInterface">
<plugin name="save_sort_code" type="MlCustomDirectDebitModelPluginOrderSave"/>
<plugin name="get_sort_code" type="MlCustomDirectDebitModelPluginOrderGet"/>
</type>
</config>
extension_attributes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoSalesApiDataOrderInterface">
<attribute code="sort_code" type="MlCustomDirectDebitApiDataMessageInterface" />
</extension_attributes>
</config>
Api/Data/MessageInterface.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitApiData;
/**
* Interface MessageInterface
* @api
*/
interface MessageInterface extends MagentoFrameworkApiExtensibleDataInterface
{
/**#@+
* Constants for keys of data array. Identical to the name of the getter in snake case
*/
// const MESSAGE = 'message';
const SORT_CODE ='sort_code';
/**
* Return the sort code.
*
* @return int|null sort code. Otherwise, null.
*/
public function getSortCode();
/**
* Set the sort code.
*
* @param string $sortCode
* @return $this
*/
public function setSortCode($sortCode);
/**
* Retrieve existing extension attributes object or create a new one.
*
* @return MlCustomDirectDebitApiDataMessageExtensionInterface|null
*/
public function getExtensionAttributes();
/**
* Set an extension attributes object.
*
* @param MlCustomDirectDebitApiDataMessageExtensionInterface $extensionAttributes
* @return $this
*/
public function setExtensionAttributes(
MlCustomDirectDebitApiDataMessageExtensionInterface $extensionAttributes
);
}
Api/OrderRepositoryInterface.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitApi;
/**
* Interface OrderRepositoryInterface
* @api
*/
interface OrderRepositoryInterface
{
/**
* Return the sort code for a specified order.
*
* @param int $orderId The order ID.
* @return MlCustomDirectDebitApiDataMessageInterface Sort code.
* @throws MagentoFrameworkExceptionNoSuchEntityException
*/
public function get($orderId);
/**
* Set the sort code for an entire order.
*
* @param int $orderId The order ID.
* @param MlCustomDirectDebitApiDataMessageInterface $sortCode The sort code.
* @return bool
* @throws MagentoFrameworkExceptionNoSuchEntityException
* @throws MagentoFrameworkExceptionInputException
* @throws MagentoFrameworkExceptionCouldNotSaveException
* @throws MagentoFrameworkExceptionStateInvalidTransitionException
*/
public function save($orderId, MlCustomDirectDebitApiDataMessageInterface $sortCode);
}
Model/Plugin/OrderGet.php
<?php
/**
*
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitModelPlugin;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderGet
{
/** @var MlCustomDirectDebitApiOrderRepositoryInterface */
protected $sortCodeOrderRepository;
/** @var MagentoSalesApiDataOrderExtensionFactory */
protected $orderExtensionFactory;
/**
*
* @Todo
* Okay, so go through this and the other file and remove any instance of "message" and change it to "sortCode"
* Make sure all the files actually exist where they need to
*/
/**
* Init plugin
*
* @param MlCustomDirectDebitApiOrderRepositoryInterface $sortcodeOrderRepository
* @param MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
*/
public function __construct(
MlCustomDirectDebitApiOrderRepositoryInterface $sortCodeOrderRepository,
MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
) {
$this->sortcodeOrderRepository = $sortCodeOrderRepository;
$this->orderExtensionFactory = $orderExtensionFactory;
}
/**
* Get sort code
*
* @param MagentoSalesApiOrderRepositoryInterface $subject
* @param MagentoSalesApiDataOrderInterface $resultOrder
* @return MagentoSalesApiDataOrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
MagentoSalesApiOrderRepositoryInterface $subject,
MagentoSalesApiDataOrderInterface $resultOrder
) {
$resultOrder = $this->getOrderSortcode($resultOrder);
return $resultOrder;
}
/**
* Get sort code for order
*
* @param MagentoSalesApiDataOrderInterface $order
* @return MagentoSalesApiDataOrderInterface
*/
protected function getOrderSortCode(MagentoSalesApiDataOrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getSortCode()) {
return $order;
}
try {
/** @var MlCustomDirectDebitApiDataMessageInterface $sortCode */
$sortCode = $this->sortCodeOrderRepository->get($order->getEntityId());
} catch (NoSuchEntityException $e) {
return $order;
}
/** @var MagentoSalesApiDataOrderExtension $orderExtension */
$orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
$orderExtension->setsortCode($sortCode);
$order->setExtensionAttributes($orderExtension);
return $order;
}
}
Model/Plugin/OrderSave.php
<?php
/**
*
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitModelPlugin;
use MagentoFrameworkExceptionCouldNotSaveException;
class OrderSave
{
/** @var MlCustomDirectDebitApiOrderRepositoryInterface */
protected $sortCodeOrderRepository;
/**
* Init plugin
*
* @param MlCustomDirectDebitApiOrderRepositoryInterface $sortCodeOrderRepository
*/
public function __construct(
MlCustomDirectDebitApiOrderRepositoryInterface $sortcodeOrderRepository
) {
$this->sortCodeOrderRepository = $sortcodeOrderRepository;
}
/**
* Save sort code
*
* @param MagentoSalesApiOrderRepositoryInterface $subject
* @param MagentoSalesApiDataOrderInterface $resultOrder
* @return MagentoSalesApiDataOrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws CouldNotSaveException
*/
public function afterSave(
MagentoSalesApiOrderRepositoryInterface $subject,
MagentoSalesApiDataOrderInterface $resultOrder
) {
/** @var MlCustomDirectDebitApiDataOrderInterface $resultOrder */
$resultOrder = $this->saveOrderSortcode($resultOrder);
return $resultOrder;
}
/**
* Save sort code for order
*
* @param MagentoSalesApiDataOrderInterface $order
* @return MagentoSalesApiDataOrderInterface
* @throws CouldNotSaveException
*/
protected function saveOrderSortCode(MagentoSalesApiDataOrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if (
null !== $extensionAttributes &&
null !== $extensionAttributes->getSortCode()
) {
/* @var MlCustomDirectDebitApiDataMessageInterface $sortCode */
$sortCode = $extensionAttributes->getSortCode();
try {
$this->sortCodeOrderRepository->save($order->getEntityId(), $sortCode);
} catch (Exception $e) {
throw new CouldNotSaveException(
__('Could not add sort code to order: "%1"', $e->getMessage()),
$e
);
}
}
return $order;
}
}
magento2 rest-api
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
EDIT: updated with more code.
I'm looking to add some more data to
/rest/VI/orders/{orderID}
through the use of extension attributes.
I have read through various ways of doing this, but they seem to have either conflicting information, or simply don't work -
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html
https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
How is it possible to set an extension_attributes to the orders and have it appear with REST/SOAP API?
I would need to take in extra information using the REST API and then have this displayed at the endpoint outlined above. The values would also need to be stored in the database, and i'm guessing retrieved from here for the endpoint.There would be nothing on the frontend for the customer to fill in - this is purely outside of Magento using the REST API to create orders.
Simply adding the extension_attributes file, should the custom_part I added be viewable at the endpoint - I understand it would be blank if so.
For setting and getting data, should the type=""
point to a file such as vendormoduleApiDataAttributeInterface
If so, what should the contents of this file be? Would I need to use a plugin to do this instead? How are these extension attributed added to the database?
Any help would this would be appreciated.
EDIT:
I have got it to be added to the endpoint on the local Swagger page. However, when i try to call the endpoint with Postman it gives a 500 error.
I have tried looking at the Magento/GiftMessage module and copied / changed some of the code to try and replicate what was happening.
My files now look like so -
di.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MlCustomDirectDebitApiOrderRepositoryInterface" type="MlCustomDirectDebitModelOrderRepository"/>
<type name="MagentoSalesApiOrderRepositoryInterface">
<plugin name="save_sort_code" type="MlCustomDirectDebitModelPluginOrderSave"/>
<plugin name="get_sort_code" type="MlCustomDirectDebitModelPluginOrderGet"/>
</type>
</config>
extension_attributes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoSalesApiDataOrderInterface">
<attribute code="sort_code" type="MlCustomDirectDebitApiDataMessageInterface" />
</extension_attributes>
</config>
Api/Data/MessageInterface.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitApiData;
/**
* Interface MessageInterface
* @api
*/
interface MessageInterface extends MagentoFrameworkApiExtensibleDataInterface
{
/**#@+
* Constants for keys of data array. Identical to the name of the getter in snake case
*/
// const MESSAGE = 'message';
const SORT_CODE ='sort_code';
/**
* Return the sort code.
*
* @return int|null sort code. Otherwise, null.
*/
public function getSortCode();
/**
* Set the sort code.
*
* @param string $sortCode
* @return $this
*/
public function setSortCode($sortCode);
/**
* Retrieve existing extension attributes object or create a new one.
*
* @return MlCustomDirectDebitApiDataMessageExtensionInterface|null
*/
public function getExtensionAttributes();
/**
* Set an extension attributes object.
*
* @param MlCustomDirectDebitApiDataMessageExtensionInterface $extensionAttributes
* @return $this
*/
public function setExtensionAttributes(
MlCustomDirectDebitApiDataMessageExtensionInterface $extensionAttributes
);
}
Api/OrderRepositoryInterface.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitApi;
/**
* Interface OrderRepositoryInterface
* @api
*/
interface OrderRepositoryInterface
{
/**
* Return the sort code for a specified order.
*
* @param int $orderId The order ID.
* @return MlCustomDirectDebitApiDataMessageInterface Sort code.
* @throws MagentoFrameworkExceptionNoSuchEntityException
*/
public function get($orderId);
/**
* Set the sort code for an entire order.
*
* @param int $orderId The order ID.
* @param MlCustomDirectDebitApiDataMessageInterface $sortCode The sort code.
* @return bool
* @throws MagentoFrameworkExceptionNoSuchEntityException
* @throws MagentoFrameworkExceptionInputException
* @throws MagentoFrameworkExceptionCouldNotSaveException
* @throws MagentoFrameworkExceptionStateInvalidTransitionException
*/
public function save($orderId, MlCustomDirectDebitApiDataMessageInterface $sortCode);
}
Model/Plugin/OrderGet.php
<?php
/**
*
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitModelPlugin;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderGet
{
/** @var MlCustomDirectDebitApiOrderRepositoryInterface */
protected $sortCodeOrderRepository;
/** @var MagentoSalesApiDataOrderExtensionFactory */
protected $orderExtensionFactory;
/**
*
* @Todo
* Okay, so go through this and the other file and remove any instance of "message" and change it to "sortCode"
* Make sure all the files actually exist where they need to
*/
/**
* Init plugin
*
* @param MlCustomDirectDebitApiOrderRepositoryInterface $sortcodeOrderRepository
* @param MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
*/
public function __construct(
MlCustomDirectDebitApiOrderRepositoryInterface $sortCodeOrderRepository,
MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
) {
$this->sortcodeOrderRepository = $sortCodeOrderRepository;
$this->orderExtensionFactory = $orderExtensionFactory;
}
/**
* Get sort code
*
* @param MagentoSalesApiOrderRepositoryInterface $subject
* @param MagentoSalesApiDataOrderInterface $resultOrder
* @return MagentoSalesApiDataOrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
MagentoSalesApiOrderRepositoryInterface $subject,
MagentoSalesApiDataOrderInterface $resultOrder
) {
$resultOrder = $this->getOrderSortcode($resultOrder);
return $resultOrder;
}
/**
* Get sort code for order
*
* @param MagentoSalesApiDataOrderInterface $order
* @return MagentoSalesApiDataOrderInterface
*/
protected function getOrderSortCode(MagentoSalesApiDataOrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getSortCode()) {
return $order;
}
try {
/** @var MlCustomDirectDebitApiDataMessageInterface $sortCode */
$sortCode = $this->sortCodeOrderRepository->get($order->getEntityId());
} catch (NoSuchEntityException $e) {
return $order;
}
/** @var MagentoSalesApiDataOrderExtension $orderExtension */
$orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
$orderExtension->setsortCode($sortCode);
$order->setExtensionAttributes($orderExtension);
return $order;
}
}
Model/Plugin/OrderSave.php
<?php
/**
*
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitModelPlugin;
use MagentoFrameworkExceptionCouldNotSaveException;
class OrderSave
{
/** @var MlCustomDirectDebitApiOrderRepositoryInterface */
protected $sortCodeOrderRepository;
/**
* Init plugin
*
* @param MlCustomDirectDebitApiOrderRepositoryInterface $sortCodeOrderRepository
*/
public function __construct(
MlCustomDirectDebitApiOrderRepositoryInterface $sortcodeOrderRepository
) {
$this->sortCodeOrderRepository = $sortcodeOrderRepository;
}
/**
* Save sort code
*
* @param MagentoSalesApiOrderRepositoryInterface $subject
* @param MagentoSalesApiDataOrderInterface $resultOrder
* @return MagentoSalesApiDataOrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws CouldNotSaveException
*/
public function afterSave(
MagentoSalesApiOrderRepositoryInterface $subject,
MagentoSalesApiDataOrderInterface $resultOrder
) {
/** @var MlCustomDirectDebitApiDataOrderInterface $resultOrder */
$resultOrder = $this->saveOrderSortcode($resultOrder);
return $resultOrder;
}
/**
* Save sort code for order
*
* @param MagentoSalesApiDataOrderInterface $order
* @return MagentoSalesApiDataOrderInterface
* @throws CouldNotSaveException
*/
protected function saveOrderSortCode(MagentoSalesApiDataOrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if (
null !== $extensionAttributes &&
null !== $extensionAttributes->getSortCode()
) {
/* @var MlCustomDirectDebitApiDataMessageInterface $sortCode */
$sortCode = $extensionAttributes->getSortCode();
try {
$this->sortCodeOrderRepository->save($order->getEntityId(), $sortCode);
} catch (Exception $e) {
throw new CouldNotSaveException(
__('Could not add sort code to order: "%1"', $e->getMessage()),
$e
);
}
}
return $order;
}
}
magento2 rest-api
EDIT: updated with more code.
I'm looking to add some more data to
/rest/VI/orders/{orderID}
through the use of extension attributes.
I have read through various ways of doing this, but they seem to have either conflicting information, or simply don't work -
http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html
https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
How is it possible to set an extension_attributes to the orders and have it appear with REST/SOAP API?
I would need to take in extra information using the REST API and then have this displayed at the endpoint outlined above. The values would also need to be stored in the database, and i'm guessing retrieved from here for the endpoint.There would be nothing on the frontend for the customer to fill in - this is purely outside of Magento using the REST API to create orders.
Simply adding the extension_attributes file, should the custom_part I added be viewable at the endpoint - I understand it would be blank if so.
For setting and getting data, should the type=""
point to a file such as vendormoduleApiDataAttributeInterface
If so, what should the contents of this file be? Would I need to use a plugin to do this instead? How are these extension attributed added to the database?
Any help would this would be appreciated.
EDIT:
I have got it to be added to the endpoint on the local Swagger page. However, when i try to call the endpoint with Postman it gives a 500 error.
I have tried looking at the Magento/GiftMessage module and copied / changed some of the code to try and replicate what was happening.
My files now look like so -
di.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MlCustomDirectDebitApiOrderRepositoryInterface" type="MlCustomDirectDebitModelOrderRepository"/>
<type name="MagentoSalesApiOrderRepositoryInterface">
<plugin name="save_sort_code" type="MlCustomDirectDebitModelPluginOrderSave"/>
<plugin name="get_sort_code" type="MlCustomDirectDebitModelPluginOrderGet"/>
</type>
</config>
extension_attributes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoSalesApiDataOrderInterface">
<attribute code="sort_code" type="MlCustomDirectDebitApiDataMessageInterface" />
</extension_attributes>
</config>
Api/Data/MessageInterface.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitApiData;
/**
* Interface MessageInterface
* @api
*/
interface MessageInterface extends MagentoFrameworkApiExtensibleDataInterface
{
/**#@+
* Constants for keys of data array. Identical to the name of the getter in snake case
*/
// const MESSAGE = 'message';
const SORT_CODE ='sort_code';
/**
* Return the sort code.
*
* @return int|null sort code. Otherwise, null.
*/
public function getSortCode();
/**
* Set the sort code.
*
* @param string $sortCode
* @return $this
*/
public function setSortCode($sortCode);
/**
* Retrieve existing extension attributes object or create a new one.
*
* @return MlCustomDirectDebitApiDataMessageExtensionInterface|null
*/
public function getExtensionAttributes();
/**
* Set an extension attributes object.
*
* @param MlCustomDirectDebitApiDataMessageExtensionInterface $extensionAttributes
* @return $this
*/
public function setExtensionAttributes(
MlCustomDirectDebitApiDataMessageExtensionInterface $extensionAttributes
);
}
Api/OrderRepositoryInterface.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitApi;
/**
* Interface OrderRepositoryInterface
* @api
*/
interface OrderRepositoryInterface
{
/**
* Return the sort code for a specified order.
*
* @param int $orderId The order ID.
* @return MlCustomDirectDebitApiDataMessageInterface Sort code.
* @throws MagentoFrameworkExceptionNoSuchEntityException
*/
public function get($orderId);
/**
* Set the sort code for an entire order.
*
* @param int $orderId The order ID.
* @param MlCustomDirectDebitApiDataMessageInterface $sortCode The sort code.
* @return bool
* @throws MagentoFrameworkExceptionNoSuchEntityException
* @throws MagentoFrameworkExceptionInputException
* @throws MagentoFrameworkExceptionCouldNotSaveException
* @throws MagentoFrameworkExceptionStateInvalidTransitionException
*/
public function save($orderId, MlCustomDirectDebitApiDataMessageInterface $sortCode);
}
Model/Plugin/OrderGet.php
<?php
/**
*
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitModelPlugin;
use MagentoFrameworkExceptionNoSuchEntityException;
class OrderGet
{
/** @var MlCustomDirectDebitApiOrderRepositoryInterface */
protected $sortCodeOrderRepository;
/** @var MagentoSalesApiDataOrderExtensionFactory */
protected $orderExtensionFactory;
/**
*
* @Todo
* Okay, so go through this and the other file and remove any instance of "message" and change it to "sortCode"
* Make sure all the files actually exist where they need to
*/
/**
* Init plugin
*
* @param MlCustomDirectDebitApiOrderRepositoryInterface $sortcodeOrderRepository
* @param MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
*/
public function __construct(
MlCustomDirectDebitApiOrderRepositoryInterface $sortCodeOrderRepository,
MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
) {
$this->sortcodeOrderRepository = $sortCodeOrderRepository;
$this->orderExtensionFactory = $orderExtensionFactory;
}
/**
* Get sort code
*
* @param MagentoSalesApiOrderRepositoryInterface $subject
* @param MagentoSalesApiDataOrderInterface $resultOrder
* @return MagentoSalesApiDataOrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
MagentoSalesApiOrderRepositoryInterface $subject,
MagentoSalesApiDataOrderInterface $resultOrder
) {
$resultOrder = $this->getOrderSortcode($resultOrder);
return $resultOrder;
}
/**
* Get sort code for order
*
* @param MagentoSalesApiDataOrderInterface $order
* @return MagentoSalesApiDataOrderInterface
*/
protected function getOrderSortCode(MagentoSalesApiDataOrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getSortCode()) {
return $order;
}
try {
/** @var MlCustomDirectDebitApiDataMessageInterface $sortCode */
$sortCode = $this->sortCodeOrderRepository->get($order->getEntityId());
} catch (NoSuchEntityException $e) {
return $order;
}
/** @var MagentoSalesApiDataOrderExtension $orderExtension */
$orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
$orderExtension->setsortCode($sortCode);
$order->setExtensionAttributes($orderExtension);
return $order;
}
}
Model/Plugin/OrderSave.php
<?php
/**
*
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MlCustomDirectDebitModelPlugin;
use MagentoFrameworkExceptionCouldNotSaveException;
class OrderSave
{
/** @var MlCustomDirectDebitApiOrderRepositoryInterface */
protected $sortCodeOrderRepository;
/**
* Init plugin
*
* @param MlCustomDirectDebitApiOrderRepositoryInterface $sortCodeOrderRepository
*/
public function __construct(
MlCustomDirectDebitApiOrderRepositoryInterface $sortcodeOrderRepository
) {
$this->sortCodeOrderRepository = $sortcodeOrderRepository;
}
/**
* Save sort code
*
* @param MagentoSalesApiOrderRepositoryInterface $subject
* @param MagentoSalesApiDataOrderInterface $resultOrder
* @return MagentoSalesApiDataOrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws CouldNotSaveException
*/
public function afterSave(
MagentoSalesApiOrderRepositoryInterface $subject,
MagentoSalesApiDataOrderInterface $resultOrder
) {
/** @var MlCustomDirectDebitApiDataOrderInterface $resultOrder */
$resultOrder = $this->saveOrderSortcode($resultOrder);
return $resultOrder;
}
/**
* Save sort code for order
*
* @param MagentoSalesApiDataOrderInterface $order
* @return MagentoSalesApiDataOrderInterface
* @throws CouldNotSaveException
*/
protected function saveOrderSortCode(MagentoSalesApiDataOrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if (
null !== $extensionAttributes &&
null !== $extensionAttributes->getSortCode()
) {
/* @var MlCustomDirectDebitApiDataMessageInterface $sortCode */
$sortCode = $extensionAttributes->getSortCode();
try {
$this->sortCodeOrderRepository->save($order->getEntityId(), $sortCode);
} catch (Exception $e) {
throw new CouldNotSaveException(
__('Could not add sort code to order: "%1"', $e->getMessage()),
$e
);
}
}
return $order;
}
}
magento2 rest-api
magento2 rest-api
edited Nov 8 '17 at 11:46
asked Oct 31 '17 at 15:35
BigDaddyL
16312
16312
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you are using the wrong way, I did that but using the observer
please follow this post
https://www.wikicoode.com/magento2/add-order-attribute-custom-attributes-magento-2-rest-api
you can add it to the json in the extension_attributes part, "extension_attributes":{"order_attribute":10}
– WISAM HAKIM
Sep 17 '18 at 15:12
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f199478%2fadding-extension-attributes-to-order-api-endpoint%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you are using the wrong way, I did that but using the observer
please follow this post
https://www.wikicoode.com/magento2/add-order-attribute-custom-attributes-magento-2-rest-api
you can add it to the json in the extension_attributes part, "extension_attributes":{"order_attribute":10}
– WISAM HAKIM
Sep 17 '18 at 15:12
add a comment |
I think you are using the wrong way, I did that but using the observer
please follow this post
https://www.wikicoode.com/magento2/add-order-attribute-custom-attributes-magento-2-rest-api
you can add it to the json in the extension_attributes part, "extension_attributes":{"order_attribute":10}
– WISAM HAKIM
Sep 17 '18 at 15:12
add a comment |
I think you are using the wrong way, I did that but using the observer
please follow this post
https://www.wikicoode.com/magento2/add-order-attribute-custom-attributes-magento-2-rest-api
I think you are using the wrong way, I did that but using the observer
please follow this post
https://www.wikicoode.com/magento2/add-order-attribute-custom-attributes-magento-2-rest-api
answered Jul 1 '18 at 2:43
WISAM HAKIM
1,553617
1,553617
you can add it to the json in the extension_attributes part, "extension_attributes":{"order_attribute":10}
– WISAM HAKIM
Sep 17 '18 at 15:12
add a comment |
you can add it to the json in the extension_attributes part, "extension_attributes":{"order_attribute":10}
– WISAM HAKIM
Sep 17 '18 at 15:12
you can add it to the json in the extension_attributes part, "extension_attributes":{"order_attribute":10}
– WISAM HAKIM
Sep 17 '18 at 15:12
you can add it to the json in the extension_attributes part, "extension_attributes":{"order_attribute":10}
– WISAM HAKIM
Sep 17 '18 at 15:12
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f199478%2fadding-extension-attributes-to-order-api-endpoint%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown