I am creating a duplicate order that has been previously placed by a specific customer. How can I set the same billing and shipping address on the new order that I am creating programatically?
1 Answer
You can load any data from the existing order object.
$order = Mage::getModel('sales/order')->load($oldOrderId);
$order->getBillingAddress()->getFirstname();
Another way would be to use the old orders quote
$order = Mage::getModel('sales/order')->load($oldOrderId);
$quote = Mage::getModel('sales/quote')->load($order->getQuoteId();
$quote->collectTotals();
$converter = Mage::getModel('sales/convert_quote');
$order = $convert->toOrder($quote)
->setPayment($converter->paymentToOrderPayment($quote->getPayment()))
->setShippingAddress($converter->addressToOrderAddress($quote->getShippingAddress()))
->setBillingAddress($converter->addressToOrderAddress($quote->getBillingAddress()));
foreach ($quote->getAllItems() as $quoteItem) {
$orderItem = $convert->itemToOrderItem($quoteItem);
if ($quoteItem->getParentItem()) {
$orderItem->setParentItem($order->getItemByQuoteItemId($quoteItem->getParentItem()->getId()));
}
$order->addItem($orderItem);
}
$order->save();
disclaimer:
1. got the code from an old script, didn't test it and might need some debuggen here and there
2. if you have the quote cleanup cron working quotes will be thrown away after 30 days so can't be loaded