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

Rest API Magento 2.4 order update overwrites existing item information #28483

Merged
merged 5 commits into from
Jun 25, 2020
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
22 changes: 18 additions & 4 deletions app/code/Magento/Sales/Model/Order/ItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,31 @@ public function deleteById($id)
public function save(OrderItemInterface $entity)
{
if ($entity->getProductOption()) {
$request = $this->getBuyRequest($entity);
$productOptions = $entity->getProductOptions();
$productOptions['info_buyRequest'] = $request->toArray();
$entity->setProductOptions($productOptions);
$entity->setProductOptions($this->getItemProductOptions($entity));
}

$this->metadata->getMapper()->save($entity);
$this->registry[$entity->getEntityId()] = $entity;
return $this->registry[$entity->getEntityId()];
}

/**
* Return product options
*
* @param OrderItemInterface $entity
* @return array
*/
private function getItemProductOptions(OrderItemInterface $entity): array
{
$request = $this->getBuyRequest($entity);
$productOptions = $entity->getProductOptions();
$productOptions['info_buyRequest'] = $productOptions && !empty($productOptions['info_buyRequest'])
? array_merge($productOptions['info_buyRequest'], $request->toArray())
: $request->toArray();

return $productOptions;
}

/**
* Set parent item.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,94 @@
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Sales\Service\V1;

use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;

/**
* Test for hold order.
*/
class OrderHoldTest extends WebapiAbstract
{
const SERVICE_VERSION = 'V1';
private const SERVICE_VERSION = 'V1';

const SERVICE_NAME = 'salesOrderManagementV1';
private const SERVICE_NAME = 'salesOrderManagementV1';

/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
}

/**
* @magentoApiDataFixture Magento/Sales/_files/order.php
* Test hold order and check order items product options after.
*
* @magentoApiDataFixture Magento/Sales/_files/order_with_two_configurable_variations.php
*
* @return void
*/
public function testOrderHold()
public function testOrderHold(): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$order = $objectManager->get(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000001');
$order = $this->objectManager->get(Order::class)
->loadByIncrementId('100000001');
$orderId = $order->getId();
$orderItemsProductOptions = $this->getOrderItemsProductOptions($order);

$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/orders/' . $order->getId() . '/hold',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
'resourcePath' => '/V1/orders/' . $orderId . '/hold',
'httpMethod' => Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'hold',
],
];
$requestData = ['id' => $order->getId()];
$requestData = ['id' => $orderId];
$result = $this->_webApiCall($serviceInfo, $requestData);
$this->assertTrue($result);

$this->assertEquals(
$orderItemsProductOptions,
$this->getOrderItemsProductOptions($this->orderRepository->get($orderId))
);
}

/**
* Return order items product options
*
* @param OrderInterface $order
* @return array
*/
private function getOrderItemsProductOptions(OrderInterface $order): array
{
$result = [];
foreach ($order->getItems() as $orderItem) {
$result[] = $orderItem->getProductOptions();
}

return $result;
}
}