2

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(); 
4
  • can you put your script?
    – Amit Bera
    Commented Sep 10, 2014 at 10:01
  • I added the script in first post.
    – aceveve
    Commented Sep 10, 2014 at 11:25
  • After a quote (cart) becomes an order, the cart will be empty. How can you reduce the value of the cart if it has already converted to an order? Commented Sep 10, 2014 at 12:41
  • Sorry, my mistake. I want to reduce the value of the order, not my cart, of course. Sorry again.
    – aceveve
    Commented Sep 10, 2014 at 12:45

3 Answers 3

2

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(); 
}

}
0

You need to call collect totals on the quote object:

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

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";
}

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.