magento
magento copied to clipboard
Delivery Options are not shown when using the Order API in 'list' mode
Describe the bug When calling on Magento's Order API you can get the Delivery Options to show when using https://example.nl/rest/V1/orders/100, but not when using Search Criteria (like https://example.nl/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=updated_at&searchCriteria[filter_groups][0][filters][0][value]=2021-02-25T10:33:44&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq&searchCriteria[page_size]=50&searchCriteria[current_page]=1).
However, most ERP connections will use the Search Criteria, because they're looking for updated orders and not a specific order.
Also, the current implementation of the Order API Plugin is not best-practice. You shouldn't use a direct DB-connection for that.
To Reproduce Steps to reproduce the behavior:
- Use Postman to call on a specific order with Delivery Options, like
GET https://example.nl/rest/V1/orders/100
- See that under extension_attributes, you have 'delivery options' shown as a JSON
- Use Postman to call on a list of orders with atleast one with Delivery Options, like
GET https://example.nl/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=updated_at&searchCriteria[filter_groups][0][filters][0][value]=2021-02-25T10:33:44&searchCriteria[filter_groups][0][filters][0][condition_type]=gteq&searchCriteria[page_size]=50&searchCriteria[current_page]=1
- Delivery Options is shown as an empty field.
Expected behavior Delivery options to show in both specific and list modes of the order API
Solution For this client I've made my own plugin (and extended the extension_attributes.xml a little bit):
<?php
declare(strict_types=1);
namespace EXAMPLE\OrderApiExtendMyParcel\Plugin\Webapi\Magento\Sales\Api;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderInterface;
class OrderRepositoryInterface
{
/**
* @var OrderExtensionFactory
*/
protected $orderExtensionFactory;
/**
* OrderRepositoryInterface constructor.
*
* @param OrderExtensionFactory $orderExtensionFactory
*/
public function __construct(
OrderExtensionFactory $orderExtensionFactory
) {
$this->orderExtensionFactory = $orderExtensionFactory;
}
public function afterGet(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
$result
) {
$result = $this->getMyParcelDeliveryOptions($result);
return $result;
}
public function afterGetList(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Magento\Sales\Model\ResourceModel\Order\Collection $result
) {
foreach ($result->getItems() as $order) {
$this->afterGet($subject, $order);
}
return $result;
}
protected function getMyParcelDeliveryOptions(OrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getDeliveryOptions()) {
return $order;
}
$deliveryOptions = $order->getData('myparcel_delivery_options');
if($deliveryOptions) {
$orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
$orderExtension->setMyparcelDeliveryOptions($deliveryOptions);
$order->setExtensionAttributes($orderExtension);
$deliveryDateObject = json_decode($deliveryOptions);
$deliveryDate = date('Y-m-d', strtotime($deliveryDateObject->date));
if($deliveryDate) {
$orderExtension->setMyparcelDeliveryDate($deliveryDate);
}
$order->setExtensionAttributes($orderExtension);
}
return $order;
}
}