从现有订单中删除项目。
从 magento 根目录运行以下代码行作为外部代码:
- 将其保存为“order_item_remove.php”
- 以http://example.com/order_item_remove.php运行它
代码块如下:
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
try{
$_order = Mage::getModel('sales/order')->loadByIncrementId('100000011'); // Order Increment Id
$items = $_order->getAllItems();
foreach ($items as $item){
Mage::log('All items ' . print_r($item->getData(), true), null, 'items_data.log');
$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();
Mage::log('Items ' . $item->getSku(), null, 'items_data.log');
if($item->getSku()=='SAMPLEKIT'){
$item_price = $item->getPrice();
$item_tva = $item->getTaxAmount();
Mage::log('Item deleted ' . $item->getSku(), null, 'items_data.log');
$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();
}
}
echo "success";
} catch(Exception $e) {
echo "failed";
}