Skip to content

Instantly share code, notes, and snippets.

@amacgregor
Created September 8, 2011 14:08
Show Gist options
  • Save amacgregor/1203480 to your computer and use it in GitHub Desktop.
Save amacgregor/1203480 to your computer and use it in GitHub Desktop.
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app();
//Test Variables
$item['sku'] = 'Avent-Soothie-AD-SCF190/01';
$item['qty'] = 2;
$orderIncrementId = '100003301';
$storeId = 1;
$store = Mage::app()->getStore($storeId);
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$quote = Mage::getModel('sales/quote')->load($order->getQuoteId());
$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 = $product->getPrice();
$qty = $item['qty'];
//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;
$grandTotal;
$taxTotal;
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();
?>
<pre><?php print_r($order) ?></pre>
<pre><?php echo($subTotal) ?></pre>
<pre><?php echo($taxTotal) ?></pre>
<pre><?php echo($grandTotal) ?></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment