1

Below is the code which i am using right now, but it not working.

  <?php
    define('MAGENTO', realpath(dirname(__FILE__) . "/.."));
    require_once MAGENTO . '/app/Mage.php';
    umask(0);
    Mage::init();
    Mage::app()->addEventArea('global');
    Mage::getSingleton('core/session', array('name' => 'frontend'));
    
    $orders = Mage::getModel('sales/order')->getCollection()
        ->addFieldToFilter('created_at', array(
            'from'     => strtotime('-1 day', time()),
            'to'       => time(),
            'datetime' => true,
        ))->load();
    $orders_to_check = array();
    foreach ($orders as $order) {
        $order_id = $order->getRealOrderId();
        foreach ($order->getAllItems() as $item) {
            echo $item->getSku();
            if($item->getSku() == 'S343697'){
               $item->delete();
               $orders->save();
            } 
        }
    }
    print_r($orders_to_check);exit;

any thoughts how to remove item from order ?

1 Answer 1

1

Try the below code and check if it helps! I have also added the code to remove the item price from the order total as well.

<?php

define('MAGENTO', realpath(dirname(__FILE__) . "/.."));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::init();
Mage::app()->addEventArea('global');
Mage::getSingleton('core/session', array('name' => 'frontend'));

$orders = Mage::getModel('sales/order')->getCollection();
$orders->addFieldToFilter('created_at', array(
    'from'     => strtotime('-1 day', time()),
    'to'       => time(),
    'datetime' => true,
));

//return count($orders);
//return count($orders->getData());

foreach ($orders as $order) {
    $order_id = $order->getRealOrderId();

    $base_grand_total = $order->getBaseGrandTotal();
    $base_subtotal = $order->getBaseSubtotal();
    $grand_total = $order->getGrandTotal();
    $subtotal = $order->getSubtotal();
    $tax_amount = $order->getTaxAmount();
    $base_subtotal_incl_tax = $order->getBaseSubtotalInclTax();
    $subtotal_incl_tax = $order->getSubtotalInclTax();
    $total_item_count = $order->getTotalItemCount();

    foreach ($order->getAllItems() as $item) {
        //echo $item->getSku();
        if($item->getSku() == 'S343697'){
            
            $item_price = $item->getPrice();
            $item_tax_amount = $item->getTaxAmount();
            $item->delete();
 
            //remove item price from Order price and update the Order totals.
            $order->setBaseGrandTotal($base_grand_total-$item_price);
            $order->setBaseSubtotal($base_subtotal-$item_price);
                $order->setGrandTotal($grand_total-$item_price);
            $order->setSubtotal($subtotal-$item_price);
            $order->setTaxAmount($tax_amount-$item_tax_amount);
            $order->setBaseSubtotalInclTax($base_subtotal_incl_tax-$item_price);
            $order->setSubtotalInclTax($subtotal_incl_tax-$item_price);
            $order->setTotalItemCount($total_item_count-1);

            $orders->save();
        } 
    }
}
?>

Hope this helps!

2

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.