magento 2 override Magento\Sales\Model\Order.php get method for REST api to add some details.
i am getting an error in di compile
Fatal Error: 'Uncaught TypeError: Argument 1 passed to Custom\\Sales\\Model\\OrderRepository::setShippingAssignments() must be an instance of Custom\\Sales\\Api\\Data\\OrderInterface, instance of Magento\\Sales\\Model\\Order\\Interceptor given
/rest/V1/order/:orderID
webapi.xml
<route url="/V1/order/:id" method="GET">
<service class="Magento\Sales\Api\OrderRepositoryInterface" method="get"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>
di.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Custom\Sales\Api\OrderRepositoryInterface" type="Custom\Sales\Model\OrderRepository"/>
<preference for="Custom\Sales\Api\Data\OrderInterface" type="Custom\Sales\Model\Order"/>
<type name="Custom\Sales\Model\OrderRepository">
<arguments>
<argument name="metadata" xsi:type="object">orderMetadata</argument>
</arguments>
</type>
<virtualType name="orderMetadata" type="Magento\Sales\Model\ResourceModel\Metadata">
<arguments>
<argument name="resourceClassName" xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument>
<argument name="modelClassName" xsi:type="string">Custom\Sales\Model\Order</argument>
</arguments>
</virtualType>
Custom\Mobileshop\Api\OrderRepositoryInterface.php
<?php
namespace Custom\Mobileshop\Api;
interface OrderRepositoryInterface
{
/**
* Loads a specified order.
*
* @param int $id The order ID.
* @return \Custom\Mobileshop\Api\Data\OrderInterface Order interface.
*/
public function get($id); }
Custom\Mobileshop\Model\OrderRepository.php
<?php
namespace Custom\Mobileshop\Model;
use Magento\Sales\Model\ResourceModel\Order as Resource;
use Magento\Sales\Model\ResourceModel\Metadata;
use Magento\Sales\Model\Order\ShippingAssignmentBuilder;
use Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory as SearchResultFactory;
use Magento\Sales\Api\Data\OrderExtensionInterface;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Custom\Mobileshop\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\ShippingAssignmentInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Api\SortOrder;
/**
* Repository class for @see OrderInterface
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/class OrderRepository implements \Custom\Mobileshop\Api\OrderRepositoryInterface{
/**
* @var Metadata
*/
protected $metadata;
/**
* @var SearchResultFactory
*/
protected $searchResultFactory = null;
/**
* @var OrderExtensionFactory
*/
private $orderExtensionFactory;
/**
* @var ShippingAssignmentBuilder
*/
private $shippingAssignmentBuilder;
/**
* \Custom\Mobileshop\Api\Data\OrderInterface[]
*
* @var array
*/
protected $registry = [];
/**
* OrderRepository constructor.
*
* @param Metadata $metadata
* @param SearchResultFactory $searchResultFactory
*/
public function __construct(
Metadata $metadata,
SearchResultFactory $searchResultFactory
) {
$this->metadata = $metadata;
}
/**
* load entity
*
* @param int $id
* @return \Custom\Mobileshop\Api\Data\OrderInterface
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function get($id)
{
if (!$id) {
throw new InputException(__('Id required'));
}
if (!isset($this->registry[$id])) {
/** @var \Custom\Mobileshop\Api\Data\OrderInterface $entity */
$entity = $this->metadata->getNewInstance()->load($id);
/*$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$entity = $objectManager->create('\Magento\Sales\Model\Order')->load($id);*/
if (!$entity->getEntityId()) {
throw new NoSuchEntityException(__('Requested entity doesn\'t exist'));
}
// $this->setShippingAssignments($entity);
$this->registry[$id] = $entity;
}
return $this->registry[$id];
}
}
Custom\Mobileshop\Api\Data\OrderInterface.php Custom\Mobileshop\Model\Order.php