Skip to content

Commit

Permalink
Add endpoint to get order from quote id
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolenorman committed Aug 8, 2024
1 parent 2f05094 commit df25618
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Api/Order/GetOrderFromQuoteIdInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bold\Checkout\Api\Order;

use Magento\Sales\Api\Data\OrderInterface;

/**
* Gets the order from the quote id.
*/
interface GetOrderFromQuoteIdInterface
{
/**
* @param int $quoteId
* @return \Magento\Sales\Api\Data\OrderInterface
*/
public function getOrder(int $quoteId): OrderInterface;
}
52 changes: 52 additions & 0 deletions Model/Order/GetOrderFromQuoteId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

namespace Bold\Checkout\Model\Order;

use Bold\Checkout\Api\Order\GetOrderFromQuoteIdInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

class GetOrderFromQuoteId implements GetOrderFromQuoteIdInterface
{
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

public function __construct(
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* @param int $quoteId
* @return OrderInterface
* @throws LocalizedException
*/
public function getOrder(int $quoteId): OrderInterface
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(OrderInterface::QUOTE_ID, $quoteId)
->create();

$ordersList = $this->orderRepository->getList($searchCriteria);

if ($ordersList->getTotalCount() === 0) {
throw new LocalizedException(__('No order found with quote id="%1"', $quoteId));
}

$orders = $ordersList->getItems();
return reset($orders);
}
}
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<preference for="Bold\Checkout\Api\Data\ModuleVersion\ResultInterface" type="Bold\Checkout\Model\Data\ModuleVersion\Result"/>
<preference for="Bold\Checkout\Api\Data\ModuleVersion\ModuleVersionInterface" type="Bold\Checkout\Model\Data\ModuleVersion\ModuleVersion"/>
<preference for="Bold\Checkout\Model\Order\CompleteOrderInterface" type="Bold\Checkout\Model\Order\CompleteOrderPool"/>
<preference for="Bold\Checkout\Api\Order\GetOrderFromQuoteIdInterface" type="Bold\Checkout\Model\Order\GetOrderFromQuoteId"/>
<type name="Bold\Checkout\Model\Order\CompleteOrderPool">
<arguments>
<argument name="pool" xsi:type="array">
Expand Down
6 changes: 6 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<resource ref="Magento_Sales::create"/>
</resources>
</route>
<route method="GET" url="/V1/orders/quote/:quoteId">
<service class="Bold\Checkout\Api\Order\GetOrderFromQuoteIdInterface" method="getOrder"/>
<resources>
<resource ref="Magento_Cart::manage"/>
</resources>
</route>
<route method="GET" url="/V1/orders/:publicOrderId/quote/:quoteMaskId/authorizeAndPlace">
<service class="Bold\Checkout\Api\PlaceOrderInterface" method="authorizeAndPlace"/>
<resources>
Expand Down

0 comments on commit df25618

Please sign in to comment.