I am trying to edit Order that includes the below possible cases:

  • Update Order Item Price
  • Increse Order Item Qty Or Cancel Order Item Qty
  • Add New Item into Order

For Adding new Item in Order, I am following the below Way:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $this->_orderFactory->load('3445');
$order = $objectManager->create('Magento\Sales\Model\Order')->load($orderId);

$quote = $objectManager->create('\Magento\Quote\Model\Quote')->load($order->getQuoteId());

$productId = 4272;
$productQty = 2;

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);

//Adding New Item into Quote

//Quote Address Update
$shippingData = $order->getShippingAddress()->getData();
$quoteAddress = $quote->getShippingAddress();
$shippingData['address_id'] = $quoteAddress->getAddressId();

$billingData = $order->getBillingAddress()->getData();
$quoteAddress = $quote->getBillingAddress();
$billingData['address_id'] = $quoteAddress->getAddressId();

//Quote Collect Totals

then I am planning to add this new Quote Item to convert into Order Item. But using the above code, All Quote Items are deleted and all grand total and subtotal in quote set to 0.

My question is: What is correct way to modify the existing Order ?

Like in sales_order_item table we have the field qty_canceled, Using the below code I can cancel the complete Item ordered qty. But when I am saving the order, Subtotal and Grand total all are set to 0.

$order = $this->_orderFactory->load('3432');
if ($order->canCancel()) {
    $orderItems = $order->getAllItems();        
    foreach ($orderItems as $item) {
        if($item['product_id']==3341) {    

  • 2
    I have not read your code in detail, but on a general level: From a business logic standpoint editing an existing order should be restricted / avoided as it is tied to accounting / book keeping. Respecting accounting standards, the correct way is to create some kind of order that will adjust the existing order (cancel, add, change price etc). Feb 1, 2018 at 10:24
  • Yes I understand but as per application logic its required Feb 5, 2018 at 9:02
  • I don't know if you are OK with using any extension for this. In case you are, there is one from FMEextensions that offers this. It allows you to modify ordered items and quantities along with other order details without canceling it. Jan 17, 2020 at 10:49

5 Answers 5


In my experience it is possible to edit the billing address, shipping address, or status of an order.

For all other actions you would have to cancel and reissue the order. When you edit an order it will append a number to the order number.

What would be the problem with changing the order number from NNNNN to NNNNN-1?


My question is: What is correct way to modify the existing Order ?

I don't think there is one as that breaks business logic as stated in comments.

Anyway Magento\Sales\Model\Order has a public function addItem(\Magento\Sales\Model\Order\Item $item). I'd try that one.

  • yes using addItem function, I am able to add product into existing order. But before that I want to add product in existing quote and adding new product in quote remove existing products in quote. Feb 6, 2018 at 5:53
  • Try using addProduct of Quote class?
    – steros
    Feb 6, 2018 at 7:31
  • Yes tried using $quote->addProduct($product, $qty); and $objectManager->create('Magento\Quote\Model\Quote\Item')->setProduct($product)->setQuote($quote)->setQty($productQty)->save() using Quote Item also Feb 6, 2018 at 8:59
  • Then that's because it's inherent logic. I didn't look at the code in detail though. It would mean you need to code it yourself or change the core code so it doesn't delete it.
    – steros
    Feb 6, 2018 at 9:12
  • I think you should look at magento's default functionality implemented to create admin order. Take a look at class Magento\Sales\Controller\Adminhtml\Order\Create. Feb 9, 2018 at 12:54

I would totally disencourage you to do that. But if you want to know how JUST in terms of learning

That being said

Take a look into these interfaces




You can use save, delete and so on, in order to achieve whatever you want. But I am pretty sure that this is not going to work properly.


  • Thanks for your answer. But I am trying the code in standalone script. I already checked the repository. but here my questions is how to modify the existing quote because modifying that remove the old items Feb 9, 2018 at 16:05
  • Handle all this is stuff is very complicated. I am not sure if Magento was designed to handle this kind of behaviour. You can take a look at the Quote Interfaces as well. Good luck! Feb 9, 2018 at 17:16

For update Order Data (price,discount,etc) you need to update qoute after add product/s somtiling like:


If you would like to modify orders, you should update quote and order.

Edit items:

$item = $quote->getItemById($id);
    if(!$item) return;

    $taxAmount = $item->getTaxPercent() * $qty * ($item->getProduct()->getPrice()/100);
    $rowTotalInclTax = $item->getPrice() * $qty + $taxAmount;
    $existData = $order->getItemByQuoteItemId($item->getId());
    $orderItem = $this->toOrderItem->convert($item);

Remove items:

foreach ($quote->getAllVisibleItems() as $quoteItem) {
        if ($id == $quoteItem->getId()){

            foreach ($order->getItemsCollection() as $orderItem){
                if($orderItem->getQuoteItemId() == $id){

Add new Items:

$product = $this->productRepository->getById($productId);
    if(!$product) return;

    $params = [
        'product'   => $productId,
        'qty'       => $qty
    $request = new \Magento\Framework\DataObject();
    $quote->addProduct($product, $request);

    foreach ($quote->getAllVisibleItems() as $item){
        if($productId == $item->getProduct()->getId()){
             * collect item price
            $existData = $order->getItemByQuoteItemId($item->getId());
            $orderItem = $this->toOrderItem->convert($item);

            if ($existData) {
                 * add new product to
                if ($item->getParentItem()) {

Pls visit my link as an example tutorial modify exist an order.

  • In which files we have to write this code, can you pls update the file paths? Mar 3, 2020 at 9:31

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