Magento reorder with existing order price
-
28-09-2020 - |
Question
I have a requirement where an order needs to be created from an existing order i.e Re-order feature of magento but programatically. A cron job dispatches a reorder event based on certain conditions. The below code looks up the order-id and creates a new order successfully. However, the new order picks the updated price of the product whereas I need it to be the same as the previous order. How can I modify it to set pricing from the last order?
$order = Mage::getModel('sales/order')->loadByIncrementId($lastOrderId);
$order_model = Mage::getSingleton('adminhtml/sales_order_create');
try {
$order->setReordered(true);
$order->getPayment()->getMethodInstance()->getInfoInstance();
$order_model->getSession()->setUseOldShippingMethod(true);
$reorder = $order_model->initFromOrder($order);
$newOrder = $reorder->createOrder();
$newOrderId = $newOrder->getIncrementId();
} catch (Exception $e) {
Mage::log('Renewal for Profile #'.$profileId.': '.$e, null, 'prosub-debug.log');
Mage::logException($e);
}
Solution
I guess you should iterate all items of both orders and adjust the prices of each one. (take in account the row_total and the discounts applied to the original order)
If you want to display the difference you can leave the price
and apply a discount, otherwise just copy all price related fields:
- price
- base_price
- original_price
- base_original_price
This should be enough.