4

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 vendor\module\Api\Data\AttributeInterface

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="Ml\CustomDirectDebit\Api\OrderRepositoryInterface" type="Ml\CustomDirectDebit\Model\OrderRepository"/>
    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="save_sort_code" type="Ml\CustomDirectDebit\Model\Plugin\OrderSave"/>
        <plugin name="get_sort_code" type="Ml\CustomDirectDebit\Model\Plugin\OrderGet"/>
    </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="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="sort_code" type="Ml\CustomDirectDebit\Api\Data\MessageInterface" />
    </extension_attributes>
</config>

Api/Data/MessageInterface.php

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Ml\CustomDirectDebit\Api\Data;

/**
 * Interface MessageInterface
 * @api
 */
interface MessageInterface extends \Magento\Framework\Api\ExtensibleDataInterface
{
    /**#@+
     * 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 \Ml\CustomDirectDebit\Api\Data\MessageExtensionInterface|null
     */
    public function getExtensionAttributes();

    /**
     * Set an extension attributes object.
     *
     * @param \Ml\CustomDirectDebit\Api\Data\MessageExtensionInterface $extensionAttributes
     * @return $this
     */
    public function setExtensionAttributes(
        \Ml\CustomDirectDebit\Api\Data\MessageExtensionInterface $extensionAttributes
    );
}

Api/OrderRepositoryInterface.php

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Ml\CustomDirectDebit\Api;

/**
 * Interface OrderRepositoryInterface
 * @api
 */
interface OrderRepositoryInterface
{
    /**
     * Return the sort code for a specified order.
     *
     * @param int $orderId The order ID.
     * @return \Ml\CustomDirectDebit\Api\Data\MessageInterface Sort code.
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function get($orderId);

    /**
     * Set the sort code for an entire order.
     *
     * @param int $orderId The order ID.
     * @param \Ml\CustomDirectDebit\Api\Data\MessageInterface $sortCode The sort code.
     * @return bool
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     * @throws \Magento\Framework\Exception\InputException
     * @throws \Magento\Framework\Exception\CouldNotSaveException
     * @throws \Magento\Framework\Exception\State\InvalidTransitionException
     */
    public function save($orderId, \Ml\CustomDirectDebit\Api\Data\MessageInterface $sortCode);
}

Model/Plugin/OrderGet.php

<?php
/**
 *
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Ml\CustomDirectDebit\Model\Plugin;

use Magento\Framework\Exception\NoSuchEntityException;

class OrderGet
{
    /** @var \Ml\CustomDirectDebit\Api\OrderRepositoryInterface */
    protected $sortCodeOrderRepository;


    /** @var \Magento\Sales\Api\Data\OrderExtensionFactory */
    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 \Ml\CustomDirectDebit\Api\OrderRepositoryInterface $sortcodeOrderRepository
     * @param \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
     */
    public function __construct(
        \Ml\CustomDirectDebit\Api\OrderRepositoryInterface $sortCodeOrderRepository,
        \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
    ) {
        $this->sortcodeOrderRepository = $sortCodeOrderRepository;
        $this->orderExtensionFactory = $orderExtensionFactory;
    }

    /**
     * Get sort code
     *
     * @param \Magento\Sales\Api\OrderRepositoryInterface $subject
     * @param \Magento\Sales\Api\Data\OrderInterface $resultOrder
     * @return \Magento\Sales\Api\Data\OrderInterface
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGet(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
        \Magento\Sales\Api\Data\OrderInterface $resultOrder
    ) {
        $resultOrder = $this->getOrderSortcode($resultOrder);

        return $resultOrder;
    }

    /**
     * Get sort code for order
     *
     * @param \Magento\Sales\Api\Data\OrderInterface $order
     * @return \Magento\Sales\Api\Data\OrderInterface
     */
    protected function getOrderSortCode(\Magento\Sales\Api\Data\OrderInterface $order)
    {
        $extensionAttributes = $order->getExtensionAttributes();
        if ($extensionAttributes && $extensionAttributes->getSortCode()) {
            return $order;
        }

        try {
            /** @var \Ml\CustomDirectDebit\Api\Data\MessageInterface $sortCode */
            $sortCode = $this->sortCodeOrderRepository->get($order->getEntityId());
        } catch (NoSuchEntityException $e) {
            return $order;
        }

        /** @var \Magento\Sales\Api\Data\OrderExtension $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 Ml\CustomDirectDebit\Model\Plugin;

use Magento\Framework\Exception\CouldNotSaveException;

class OrderSave
{
    /** @var \Ml\CustomDirectDebit\Api\OrderRepositoryInterface */
    protected $sortCodeOrderRepository;



    /**
     * Init plugin
     *
     * @param \Ml\CustomDirectDebit\Api\OrderRepositoryInterface $sortCodeOrderRepository
     */
    public function __construct(
        \Ml\CustomDirectDebit\Api\OrderRepositoryInterface $sortcodeOrderRepository

    ) {
        $this->sortCodeOrderRepository = $sortcodeOrderRepository;
    }

    /**
     * Save sort code
     *
     * @param \Magento\Sales\Api\OrderRepositoryInterface $subject
     * @param \Magento\Sales\Api\Data\OrderInterface $resultOrder
     * @return \Magento\Sales\Api\Data\OrderInterface
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     * @throws CouldNotSaveException
     */
    public function afterSave(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
        \Magento\Sales\Api\Data\OrderInterface $resultOrder
    ) {
        /** @var \Ml\CustomDirectDebit\Api\Data\OrderInterface $resultOrder */
        $resultOrder = $this->saveOrderSortcode($resultOrder);

        return $resultOrder;
    }

    /**
     * Save sort code for order
     *
     * @param \Magento\Sales\Api\Data\OrderInterface $order
     * @return \Magento\Sales\Api\Data\OrderInterface
     * @throws CouldNotSaveException
     */
    protected function saveOrderSortCode(\Magento\Sales\Api\Data\OrderInterface $order)
    {
        $extensionAttributes = $order->getExtensionAttributes();
        if (
            null !== $extensionAttributes &&
            null !== $extensionAttributes->getSortCode()
        ) {
            /* @var \Ml\CustomDirectDebit\Api\Data\MessageInterface $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;
    }
}
2
  • I have worked onto this question today and believe I may have some ideas as to the whole extension_attributes business can be understood .. more to the point how you can troubleshoot you issue. However, we don't see how your extension atttribute repository works and I think this could be what is in the way of your implmentation Commented May 27, 2020 at 15:54
  • Are you able to solve this? because I tried this tigren.com/blog/add-extension-attribute-magento-2/… but I got errors Commented Aug 25, 2022 at 9:36

1 Answer 1

0

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

1
  • you can add it to the json in the extension_attributes part, "extension_attributes":{"order_attribute":10} Commented Sep 17, 2018 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.