3

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();
5
  • what happens exactly when you use the code in the first example, let's say with the call of merge method? Does it create a new quote and saves it to database or not? Does it create some new quote items? What happens when you call the submitAll method?
    – HelgeB
    Commented Mar 27, 2019 at 12:53
  • initially empty quote is created without items, then I tried to merge with existing quote and it is not copying existing quote data to new quote. new quote have no items data.
    – Rahul
    Commented Mar 27, 2019 at 13:36
  • The merge usually works, but the items have to be salable otherwise the items won't be saved. Maybe that could be a reason.
    – HelgeB
    Commented Mar 27, 2019 at 14:56
  • Items are saleable I have checked inventory as well. Is it due to a bundle product on original order?
    – Rahul
    Commented Mar 28, 2019 at 4:02
  • normally the merge method takes care about parent / child relations (which by the way is not the case in your code). I would suggest to use merge and debug what happens in the foreach loop in the merge method by adding some usefull Mage::log lines
    – HelgeB
    Commented Mar 28, 2019 at 12:39

1 Answer 1

0

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.

3
  • But it will completely create new order, before that I want to change item and prices from quote so new order have new changes. Can you please provide code sample?
    – Rahul
    Commented Mar 20, 2019 at 17:07
  • As per this link magento.stackexchange.com/questions/12272/… It is not adding items in order when we use reorder.
    – Rahul
    Commented Mar 20, 2019 at 17:56
  • I have tried reorder() but unfortunately not adding items in new quote.
    – Rahul
    Commented Mar 21, 2019 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.