Skip to content

Instantly share code, notes, and snippets.

@amacgregor
Created September 21, 2011 15:57
Show Gist options
  • Save amacgregor/1232454 to your computer and use it in GitHub Desktop.
Save amacgregor/1232454 to your computer and use it in GitHub Desktop.
OrderEdit APi
<?php
/**
* Edit Order Items.
*
* @param $orderIncrementId
* @param $orderItems
* @param $comment
* @return string
*/
public function salesOrderEdit( $orderItems, $orderIncrementId, $comment)
{
/*Mage::log('OrderIncrementId: ' . $orderIncrementId, null, 'demac_superapi.log');
Mage::log('orderItems: ' . $orderItems, null, 'demac_superapi.log');
Mage::log('comment: ' . $comment, null, 'demac_superapi.log');
*/
// Lets load the order
$storeId = 1;
$store = Mage::app()->getStore($storeId);
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$quote = Mage::getModel('sales/quote')->load($order->getQuoteId());
$logComment = '';
try{
foreach($orderItems as $item)
{
Mage::log('orderItem: ' . $item->sku, null, 'demac_superapi.log');
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$item->sku);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)
->setQuote(Mage::getModel('sales/quote')->load($order->getQuoteId()));
$orderItem = Mage::getModel('sales/convert_quote')->itemToOrderItem($quoteItem)->setProduct($product);
$productPrice = $item->price;
$qty = $item->quantity;
// Clean Order and remove all products
$delItems = $order->getAllItems();
foreach ($delItems as $delItem)
{
$delItem->delete();
$logComment .= "<br />Product Deleted: " . $delItem->getSku();
$status = $order->getStatus();
$notify = 0;
$order->addStatusToHistory($status, $logComment, $notify);
$order->save();
}
$oItems = $order->getAllItems();
$subTotal = 0;
$grandTotal = 0;
$taxTotal = 0;
foreach ($oItems as $oItem)
{
$subTotal += $oItem['row_total'];
$taxTotal += $oItem['tax_amount'];
//print_r($oItem);
}
$grandTotal += $subTotal + $taxTotal + $order->getShippingAmount();
//$comment .= "Added item(SKU): " . $sku . "<br />";
$order->setSubtotal($subTotal)
->setBaseSubtotal($subTotal)
->setGrandTotal($grandTotal)
->setTaxAmount($taxTotal)
->setBaseGrandTotal($grandTotal);
$order->save();
//Tax in formation
$taxClassId = $quote->getCustomerTaxClassId();
$taxCalculationModel = Mage::getSingleton('tax/calculation');
$request = $taxCalculationModel->getRateRequest($quote->getShippingAddress(), $quote->getBillingAddress(), $taxClassId, $store);
$rate = $taxCalculationModel->getRate($request->setProductClassId($product->getTaxClassId()));
$rowTotal = $productPrice * $qty;
$tax = ($rowTotal * $rate)/100;
$orderItem->setPrice($productPrice);
$orderItem->setBasePrice($productPrice);
$orderItem->setBaseOriginalPrice($productPrice);
$orderItem->setOriginalPrice($productPrice);
$orderItem->setQtyOrdered($qty);
$orderItem->setTaxPercent($rate);
$orderItem->setTaxAmount($tax);
$orderItem->setRowTotal($rowTotal);
$orderItem->setBaseRowTotal($rowTotal);
$orderItem->setOrderId($order->getId());
$orderItem->setOrder($order);
$orderItem->save();
$order->addItem($orderItem);
$order->save();
}
$oItems = $order->getAllItems();
$subTotal = 0;
$grandTotal = 0;
$taxTotal = 0;
foreach ($oItems as $oItem)
{
$subTotal += $oItem['row_total'];
$taxTotal += $oItem['tax_amount'];
//print_r($oItem);
Mage::log('Subtotal: ' . $subTotal, null, 'demac_orderedit.log');
}
$grandTotal += $subTotal + $taxTotal + $order->getShippingAmount();
$comment .= "Added item(SKU): " . $sku . "<br />";
Mage::log('Subtotal2: ' . $subTotal, null, 'demac_orderedit.log');
Mage::log('GRandTotal: ' . $grandTotal, null, 'demac_orderedit.log');
$order->setSubtotal($subTotal)
->setBaseSubtotal($subTotal)
->setGrandTotal($grandTotal)
->setTaxAmount($taxTotal)
->setBaseGrandTotal($grandTotal);
$logComment .= "<br />Order Updated: " . $comment;
$status = $order->getStatus();
$notify = 0;
$order->addStatusToHistory($status, $logComment, $notify);
$order->save();
return 1;
}catch (Exception $e) {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment