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
$objectManager->create('Magento\Quote\Model\Quote\Item')
->setProduct($product)
->setQuote($quote)
->setQty($productQty)
->save();
//Quote Address Update
$shippingData = $order->getShippingAddress()->getData();
$quoteAddress = $quote->getShippingAddress();
$shippingData['address_id'] = $quoteAddress->getAddressId();
$quoteAddress->setData($shippingData);
$quoteAddress->save();
$billingData = $order->getBillingAddress()->getData();
$quoteAddress = $quote->getBillingAddress();
$billingData['address_id'] = $quoteAddress->getAddressId();
$quoteAddress->setData($billingData);
$quoteAddress->save();
//Quote Collect Totals
$quote->getShippingAddress()
->setShippingMethod($order->getShippingMethod())
->setCollectShippingRates(true);
$quote->setTotalsCollectedFlag(false)->collectTotals();
$quote->save();
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) {
$item->setQtyCanceled($item['qty_ordered']);
$item->save();
}
}
$order->save();
}