Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin for SalesOrderItemRepository gift message (#19093) #25946

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions app/code/Magento/GiftMessage/Model/Plugin/OrderItemGet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GiftMessage\Model\Plugin;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\GiftMessage\Api\OrderItemRepositoryInterface as GiftMessageItemRepositoryInterface;
use Magento\Sales\Api\Data\OrderItemExtensionFactory;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Api\OrderItemRepositoryInterface;

/**
* Plugin for adding gift message to order item
*/
class OrderItemGet
{

/**
* @var OrderItemExtensionFactory
*/
private $orderItemExtensionFactory;

/**
* @var GiftMessageItemRepositoryInterface
*/
private $giftMessageItemRepository;

/**
* OrderItemGet constructor.
*
* @param GiftMessageItemRepositoryInterface $giftMessageItemRepository
* @param OrderItemExtensionFactory $orderItemExtensionFactory
*/
public function __construct(
GiftMessageItemRepositoryInterface $giftMessageItemRepository,
OrderItemExtensionFactory $orderItemExtensionFactory
) {
$this->giftMessageItemRepository = $giftMessageItemRepository;
$this->orderItemExtensionFactory = $orderItemExtensionFactory;
}

/**
* Add gift message for order item
*
* @param OrderItemRepositoryInterface $subject
* @param OrderItemInterface $orderItem
* @return OrderItemInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(OrderItemRepositoryInterface $subject, OrderItemInterface $orderItem)
{
$extensionAttributes = $orderItem->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getGiftMessage()) {
return $orderItem;
}
try {
/* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
$giftMessage = $this->giftMessageItemRepository->get(
$orderItem->getOrderId(),
$orderItem->getItemId()
);
} catch (NoSuchEntityException $e) {
return $orderItem;
}
/** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */
$orderItemExtension = $extensionAttributes ?: $this->orderItemExtensionFactory->create();
$orderItemExtension->setGiftMessage($giftMessage);
$orderItem->setExtensionAttributes($orderItemExtension);

return $orderItem;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/GiftMessage/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@
</argument>
</arguments>
</type>
<type name="Magento\Sales\Api\OrderItemRepositoryInterface">
<plugin name="get_gift_message" type="Magento\GiftMessage\Model\Plugin\OrderItemGet"/>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GiftMessage\Api;

use Magento\TestFramework\TestCase\WebapiAbstract;

class OrderGetRepositoryTest extends WebapiAbstract
{
const SERVICE_VERSION = 'V1';
const SERVICE_NAME = 'salesOrderRepositoryV1';
const RESOURCE_PATH = '/V1/orders/';

/**
* @magentoDataFixture Magento/GiftMessage/_files/order_with_message.php
* @magentoConfigFixture default_store sales/gift_options/allow_order 1
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
public function testGet()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Sales\Model\Order $order */
$order = $objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000001');
$orderId = $order->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . $orderId,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Get',
],
];
$expectedMessage = [
'recipient' => 'Mercutio',
'sender' => 'Romeo',
'message' => 'I thought all for the best.',
];
$requestData = ['id' => $orderId];
$result = $this->_webApiCall($serviceInfo, $requestData);
$resultMessage = $result['extension_attributes']['gift_message'];
static::assertCount(5, $resultMessage);
unset($resultMessage['gift_message_id']);
unset($resultMessage['customer_id']);
static::assertEquals($expectedMessage, $resultMessage);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GiftMessage\Api;

use Magento\TestFramework\TestCase\WebapiAbstract;

class OrderItemGetRepositoryTest extends WebapiAbstract
{
const SERVICE_VERSION = 'V1';
const SERVICE_NAME = 'salesOrderItemRepositoryV1';
const RESOURCE_PATH = '/V1/orders/items/';

/**
* @magentoDataFixture Magento/GiftMessage/_files/order_with_message.php
* @magentoConfigFixture default_store sales/gift_options/allow_items 1
* @magentoAppIsolation enabled
* @magentoDbIsolation disabled
*/
public function testGet()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Sales\Model\Order $order */
$order = $objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000001');
$items = $order->getItems();
/** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
$orderItem = array_shift($items);
$itemId = $orderItem->getItemId();
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . $itemId,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Get',
],
];
$expectedMessage = [
'recipient' => 'Mercutio',
'sender' => 'Romeo',
'message' => 'I thought all for the best.',
];
$requestData = ['id' => $itemId];
$result = $this->_webApiCall($serviceInfo, $requestData);
$resultMessage = $result['extension_attributes']['gift_message'];
static::assertCount(5, $resultMessage);
unset($resultMessage['gift_message_id']);
unset($resultMessage['customer_id']);
static::assertEquals($expectedMessage, $resultMessage);
}
}