0

I created a custom charges on order for some specific products, Unfortunately custom charge was not updated in order total like cash on delivery,

Is there any way to update orders totals after order created without cancel order?

2

I think you can do this just like you would modify any other model instance.

$item = Mage::getModel('sales/order_item')->load(id here);
$item->setPrice(40)->setBasePrice(40)->save();

then

$order = Mage::getModel('sales/order')->load(order id here);
$order->setSubtotal(70)->setBaseSubtotal(70);
$order->setDiscountAmount(7)->setBaseDiscountAmout(7);
$order->setGrandTotal(63)->setBaseGrandTotal(63);
$order->save();

You might also need to change other values like tax, subtotal_incl_tax and others.

But this is a bad practice and you should not do it. It is better just to cancel the order and create a new one.

|improve this answer|||||
  • I was looking for Bulk order update, like COD was not there, That's why cancel order way not adopted, Thank you, Works for me. – Ram Pratap Singh Gour Oct 7 '16 at 4:54
2

Here I have created a solution to set custom item price and recalculate all the order totals.

We will need to calculate manually as the collectTotals will not work after an order is placed.

<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
echo "<pre>";

$customProductPrice = 5; // Custom Product Price For One Quantity
$item = Mage::getModel('sales/order_item')->load(15); //Order Itemid
if($item): //Check item object exists or not
    $quantity = $item->getQtyOrdered();
    $rowTotal = $customProductPrice * $quantity;

    # calculateTaxAmount
    if (!empty($item->getTaxPercent())) {
        $taxAmount = ($rowTotal * $item->getTaxPercent()) / 100;
        $item->setTaxAmount($taxAmount)
            ->setBaseTax_amount($taxAmount);
    }

    #  Price In Grid Update
    $item->setPrice($customProductPrice)
        ->setBasePrice($customProductPrice);

    #Subtotal On Order * Qty
    $item->setRowTotal($rowTotal)
        ->setBaseRowTotal($rowTotal)
        ->setRowTotalInclTax($rowTotal)
        ->setBaseRowTotalInclTax($rowTotal);

    # Apply Item Discounted Amount (Recaculated Discount)
    if (!empty($item->getDiscountPercent())) {
        $discountedAmount = $item->getRowTotal()*$item->getDiscountPercent()/100;
        $item->setDiscountAmount($discountedAmount)
            ->setBaseDiscountAmount($discountedAmount);
    }
    $item->save();

    $order = Mage::getModel('sales/order')->load(11); //Orderid
    $orderSubTotal = 0;
    $orderBaseTax = 0;
    $orderDiscountAmount = 0;
    foreach ($order->getAllVisibleItems() as $_item) {
        $orderSubTotal += $_item->getBaseRowTotal();
        $orderBaseTax += $_item->getBaseTaxAmount() + $_item->getBaseHiddenTaxAmount();
        $orderDiscountAmount += $_item->getBaseDiscountAmount();
    }

    $grandTotal = ($orderSubTotal + $order->getShippingAmount() + $orderBaseTax) - $orderDiscountAmount;

    # Update Order Totals
    $order->setSubtotal($orderSubTotal)
        ->setBaseSubtotal($orderSubTotal)
        ->setDiscountAmount($orderDiscountAmount)
        ->setBaseDiscountAmount($orderDiscountAmount)
        ->setTaxAmount($orderBaseTax)
        ->setBaseTaxAmount($orderBaseTax)
        ->setGrandTotal($grandTotal)
        ->setBaseGrandTotal($grandTotal)
        ->save();
endif;
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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