0

I tried to remove all products from a order. All products will removed from order but quantity not revert from stock.

$order_id = 1010101010;
$_order = Mage::getModel('sales/order')->load($order_id);
$items = $_order->getAllItems();
foreach ($items as $item) {
    //-- Get Order Stuff
    $base_grand_total = $_order->getBaseGrandTotal();
    $base_subtotal = $_order->getBaseSubtotal();
    $base_tva = $_order->getBaseTaxAmount();
    $grand_total = $_order->getGrandTotal();
    $subtotal = $_order->getSubtotal();
    $tva = $_order->getTaxAmount();
    $base_subtotal_incl_tax = $_order->getBaseSubtotalInclTax();
    $subtotal_incl_tax = $_order->getSubtotalInclTax();
    $total_item_count = $_order->getTotalItemCount();

    $item_price = $item->getPrice();
    $item_tva = $item->getTaxAmount();
    $item->delete(); //-- Remove Product 

    //-- Update Order Stuff
    $_order->setBaseGrandTotal($base_grand_total - $item_price - $item_tva);
    $_order->setBaseSubtotal($base_subtotal - $item_price);
    $_order->setBaseTaxAmount($base_tva - $item_tva);
    $_order->setGrandTotal($grand_total - $item_price - $item_tva);
    $_order->setSubtotal($subtotal - $item_price);
    $_order->setTaxAmount($tva - $item_tva);
    $_order->setBaseSubtotalInclTax($base_subtotal_incl_tax - $item_price);
    $_order->setSubtotalInclTax($subtotal_incl_tax - $item_price);
    $_order->setTotalItemCount(count($items) - 1);
    $_order->save(); //-- Save Order
}

I found that code on magento.stackexchange post - Delete products from order programmatically . I don't understand what is the problem, Can anyone please tell me what is the issue?

2
  • it because you are not updating inventory after manually deleting the item from order Feb 14, 2017 at 8:10
  • How to update inventory manually by order items. Can you please provide me sample. Feb 14, 2017 at 11:03

1 Answer 1

0

To update the qty you have to cancel order first and to Cancel order :

$order = Mage::getModel('sales/order')->load(12);
if ($order->canCancel()) {
    try {
    $order->cancel();

    // remove status history set in _setState
    $order->getStatusHistoryCollection(true);

    // do some more stuff here
    // ...

    $order->save();

    } catch (Exception $e) {
    Mage::logException($e);
    }
}

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.