Question

I use Magento CE 1.8.1.0 and I have developed a php script that can delete a product from an order. After the product is deleted the value of the order is the same. How can I reduce the order value with the amount of the deleted product?

My script:

$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 = $sItem->getPrice();

$item_tva = $sItem->getTaxAmount();

$sItem->delete();

$order->setBaseGrandTotal($base_grand_total-$item_price-$item_tva);

$order->setBaseSubtotal($base_subtotal);

$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($total_item_count-1);

$order->save(); 
Was it helpful?

Solution

I hope you do not need to remove all items. so there should be some condition over delete item and update order within it.

Following Works for me.

Just have done conditional update for order and delete for the item.and removed item price from base_subtotal too.

$_order = Mage::getModel('sales/order')->load(order_id);
$items = $_order->getAllItems();
foreach ($items as $item){
$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();



if($item->getSku()=='test-default'){
    $item_price = $item->getPrice();
    $item_tva = $item->getTaxAmount();
    $item->delete();
    $_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(); 
}

}

OTHER TIPS

You need to call collect totals on the quote object:

$quote->collectTotals()->save();

Just an update to @Namita answer.

Try

$_order = Mage::getModel('sales/order')->loadByIncrementId('100000053');
$items = $_order->getAllItems(); 

in the first line if the above one is not working for you. Just tried it from magento root as external code with this update. It works fine !

Please let me know if the complete lines of code required.

<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

try{
// $_order = Mage::getModel('sales/order')->load(order_id);
$_order = Mage::getModel('sales/order')->loadByIncrementId('100000053');
$items = $_order->getAllItems(); 
.
.
.
echo "success"; 
} catch(Exception $e) {
    echo "failed";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top