Magento – How to create duplicate new quote from old order quote Magento 1.7

magento-1.7ordersquotesales-order

I want to create a new order programmatically by copying an existing order's quote.
Here is what I have tried:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
set_time_limit(0);
require_once '/var/www/html/app/Mage.php';
umask(0);
ini_set('memory_limit', '-1');
Mage::app('default');
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');

$order_no = 1300000033;
$old_order = Mage::getModel('sales/order')->loadByIncrementId(1300000033);

if (!$old_order->getId()) {
    Mage::getSingleton('adminhtml/session')->addError('Invalid order id' . $order_no);
    echo 'Order Not Found';
    exit;
} else {
    $customer = getLoadedCustomer($old_order);
    $quoteId  = $old_order->getQuoteId();
    $storeId  = $old_order->getStoreId();
    $quoteOld = Mage::getModel('sales/quote')->load($quoteId);

    $quote = Mage::getModel('sales/quote');
    //$quote->merge($quoteOld);
    $quote->setStoreId($storeId)->setCustomerGroupId($customer->getGroupId())->assignCustomer($customer)->setIsActive(false)->save();

    foreach ($quoteOld->getItemsCollection() as $item) {
        echo "\nOld quote item - " . $item->getId();
        $item->setId(null); //Remove the item id, so it gets added
        $quote->addItem($item);
    }
    $quote->getPayment()->setMethod('free');
    $quote->collectTotals()->save();
    $service = Mage::getModel('sales/service_quote', $quote);
    $service->submitAll();
    $order = $service->getOrder();
    $order->save();
}

function getLoadedCustomer($old_order)
{
    $website_id = Mage::getModel('core/store')->load($old_order->getStoreId())->getWebsiteId();
    if ($old_order->getCustomerEmail()) {
        try {
            $customer = Mage::getModel("customer/customer")->setWebsiteId($website_id)->loadByEmail($old_order->getCustomerEmail());
        }
        catch (Exception $e) {
            echo "\nException - " . $e->getMessage();
        }
    }
    return $customer;
}

This is not loading the existing quote-items into the new quote.
I have tried $quote->merge($quoteOld); and $quote->addItem($item); and neither are working.

My objective is to create duplicate order with new fresh quote. So I
can change order items and prices from that quote as required. I am
working on creating duplicate order programmatically.
I have tried another way of doing this as per this link here
However sometimes it is not working and not adding a items to a new quote.
Please provide any suggestions.

Below is my product options data for each item. There are total 4 items in cart first 3 items should be bundled together and last item is optional.

Array
(
    [0] => Array
        (
            [id] => 40795672
            [productOptions] => Array
                (
                    [info_buyRequest] => Array
                        (
                            [uenc] => aHR0cDovL2F3c21pZWxlcmZyLnBhcnRzaW1wbGUuY29tLzI5NjE1MDEwdXNhb293LW1sZS1yLTEyLmh0bWw,
                            [product] => 40795672
                            [form_key] => U0TtW375MtfLo4VS
                            [related_product] =>
                            [qty] => 1
                            [serial_number] => serial_number_1234
                            [incident_number] => incident_number_1234
                            [date_of_purchased] => 2019-02-08
                            [problem] => fffffffffffffffffffffff
                            [bundle_option] => Array
                                (
                                    [27967] => 65358
                                    [27968] => 65359
                                )

                        )

                    [bundle_options] => Array
                        (
                            [27967] => Array
                                (
                                    [option_id] => 27967
                                    [label] => Repair Service
                                    [value] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [title] =>  29615010USAOOW
                                                    [qty] => 1
                                                    [price] => 200
                                                )

                                        )

                                )

                            [27968] => Array
                                (
                                    [option_id] => 27968
                                    [label] => Return Shipping
                                    [value] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [title] => Ground Return
                                                    [qty] => 1
                                                    [price] => 0
                                                )

                                        )

                                )

                        )

                    [product_calculations] => 0
                    [shipment_type] => 1
                )

        )

    [1] => Array
        (
            [id] => 40795671
            [productOptions] => Array
                (
                    [info_buyRequest] => Array
                        (
                            [uenc] => aHR0cDovL2F3c21pZWxlcmZyLnBhcnRzaW1wbGUuY29tLzI5NjE1MDEwdXNhb293LW1sZS1yLTEyLmh0bWw,
                            [product] => 40795672
                            [form_key] => U0TtW375MtfLo4VS
                            [related_product] =>
                            [qty] => 1
                            [serial_number] => serial_number_1234
                            [incident_number] => incident_number_1234
                            [date_of_purchased] => 2019-02-08
                            [problem] => fffffffffffffffffffffff
                            [bundle_option] => Array
                                (
                                    [27967] => 65358
                                    [27968] => 65359
                                )

                        )

                    [bundle_selection_attributes] => a:4:{s:5:"price";d:200;s:3:"qty";d:1;s:12:"option_label";s:14:"Repair Service";s:9:"option_id";s:5:"27967";}
                )

        )

    [2] => Array
        (
            [id] => 25492744
            [productOptions] => Array
                (
                    [info_buyRequest] => Array
                        (
                            [uenc] => aHR0cDovL2F3c21pZWxlcmZyLnBhcnRzaW1wbGUuY29tLzI5NjE1MDEwdXNhb293LW1sZS1yLTEyLmh0bWw,
                            [product] => 40795672
                            [form_key] => U0TtW375MtfLo4VS
                            [related_product] =>
                            [qty] => 1
                            [serial_number] => serial_number_1234
                            [incident_number] => incident_number_1234
                            [date_of_purchased] => 2019-02-08
                            [problem] => fffffffffffffffffffffff
                            [bundle_option] => Array
                                (
                                    [27967] => 65358
                                    [27968] => 65359
                                )

                        )

                    [bundle_selection_attributes] => a:4:{s:5:"price";d:0;s:3:"qty";d:1;s:12:"option_label";s:15:"Return Shipping";s:9:"option_id";s:5:"27968";}
                )

        )

    [3] => Array
        (
            [id] => 37509659
            [productOptions] => Array
                (
                    [info_buyRequest] => Array
                        (
                            [product] => 37509659
                            [qty] => 1
                            [options] => Array
                                (
                                    [1] => 1
                                )

                        )

                )

        )

)

I have tried below reorder method as well, this is also not working –

    <?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
set_time_limit(0);
require_once '/var/www/html/app/Mage.php';
umask(0);
ini_set('memory_limit', '-1');
Mage::app('default');
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
Mage::getSingleton('core/session', array('name' => 'frontend'));  
$session = Mage::getSingleton('customer/session'); 
$order_no= 1300000033;
$order = Mage::getModel('sales/order')->loadByIncrementId($order_no);
//echo "\nOld Quote id - " . $quoteId = $order->getQuoteId();
$shippingLabelProductId = '24358270';
$cartonProductId = '37509659';
$problem = "New problem ";
$shippingtorepaircenter = '0';
$shippingtobacktoyou = '';
$fedexhelper = '';

$orderBillingAddress = $order->getBillingAddress();
$orderShippingAddress = $order->getShippingAddress();
$shippingMethod = $order->getShippingMethod();
$paymentMethod = 'free';
$old_quote = $order->getQuote();
//echo "\nOld Quote id - ". $old_quote->getId();

$model = Mage::getSingleton('adminhtml/sales_order_create');
$order->setReordered(true);
$reorder = $model->initFromOrder($order);
$reorder->getQuote()->getShippingAddress()
        ->setShippingMethod($order->getShippingMethod())
        ->setCollectShippingRates(true)
        ->collectShippingRates()
        ->collectTotals()
        ->save();
$newQuote = $reorder->getQuote();
$reorder->createOrder();

Best Answer

I don’t have the code available (on my phone), but what you should do is to use the reorder functionality. That effectively will add all order items to the quote with the same configurations and quantities. Not sure about the exact code, but by looking at the reorder controller action it should hopefully be rather straightforward to implement as required.