4

I want to edit any order programmatically.

The tutorials that I am getting is canceling current order and creating new one. But I need something different, I want to edit it without canceling or creating new one.

There is a similar post here Magento 2: How to edit existing Order without cancel? But there is not accepted answer on that post.

Please help me find solution on this.

2

1 Answer 1

1

In Magento 2, you can programmatically edit an order without canceling or creating a new one by using the order repository and service classes. Here are the general steps you can follow:

  1. Use the \Magento\Sales\Api\OrderRepositoryInterface class to load the order you want to edit by its ID or increment ID.
$orderId = 1;
$order = $this->orderRepository->get($orderId);
  1. Use the \Magento\Sales\Api\Data\OrderInterface class to update the order's data.
$order->setShippingAddress($newShippingAddress);
$order->setBillingAddress($newBillingAddress);
  1. Use the \Magento\Sales\Api\OrderRepositoryInterface class to save the updated order.
$this->orderRepository->save($order);

It's important to note that, depending on the type of change you want to make, some adjustments might be necessary, such as recalculating the order totals, updating the order status, or updating the order items.

You should also make sure that the changes you are making are valid and that they don't break any rules or constraints in the order management system.

You can also use the \Magento\Sales\Api\OrderManagementInterface class to update the order's status, shipping and billing address, payment method, and other information.

$this->orderManagement->setShippingAddress($orderId, $newShippingAddress);
$this->orderManagement->setBillingAddress($orderId, $newBillingAddress);
$this->orderManagement->setPaymentMethod($orderId, $newPaymentMethod);

It's important to check the documentation of the Order Management and Order Repository classes to understand the methods and properties that are available, and also check the expected input and output of those methods before using them.

I hope it will help !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.