Skip to content

Commit

Permalink
🔃 [Magento Community Engineering] Community Contributions - 2.4-develop
Browse files Browse the repository at this point in the history
Accepted Community Pull Requests:
 - #28483: Rest API Magento 2.4 order update overwrites existing item information (by @engcom-Charlie)


Fixed GitHub Issues:
 - #22431: Rest API Magento 2.4 order update overwrites existing item information (reported by @aschrammel) has been fixed in #28483 by @engcom-Charlie in 2.4-develop branch
   Related commits:
     1. a922551
     2. beb41d0
     3. 3115fe9
     4. 15d20cb
     5. ae36ae4
  • Loading branch information
magento-engcom-team authored Jun 25, 2020
2 parents 86ec99b + b22be3c commit def5104
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 13 deletions.
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;
}
}

0 comments on commit def5104

Please sign in to comment.