0

I'm writing an subscription model that reorders specific orders a preset amount of times. My reorder function works fine as long as it is only run once, but if several orders are created in a loop, the cart contents aren't cleared between creation, causing the new orders to have the previous orders items added on each run along with the items on the original order.

This is the function I use for the reorder:

    public function reorder($subscription)
{
    // Trying to clear the session data
    Mage::getSingleton('adminhtml/session_quote')->clear();

    //Load the bases order and the model for the order create
    $order = Mage::getModel('sales/order')->loadByIncrementId($subscription->getBaseOrderId());
    $model = Mage::getSingleton('adminhtml/sales_order_create');

    //Set reorder mode and create new quote
    $order->setReordered(true);
    $reorder = $model->initFromOrder($order);

    //Create the order
    $newOrder = $reorder->createOrder();

    //Get new Increment ID and add meetingdetails to new order
    $newId = $newOrder->getIncrementId();
    $this->addMeetingDetails($order, $newId, $subscription);
}

The Mage::getSingleton('adminhtml/session_quote')->clear(); that is called first does not accomplish what I hoped for.

The following screenshot of the order list displays what happens Screenshot Could anyone advise me on how I clear the adminhtml cart contents at each run, to ensure the cart content of the first order isn't added to the following orders?

1 Answer 1

0

I ran into the same problem and came up with the not-pretty-needs-work following code:

// Create the new order
$creator = Mage::getSingleton('adminhtml/sales_order_create');
$creator->getSession()->clear();
$creator->getSession()->setCustomer($customer);
$creator->getSession()->setCustomerId(false);
$creator->getSession()->setQuote(null);
$creator->getSession()->setStoreId($customer->getStoreId()); // Set active store
$creator->getSession()->getStore(); // Initialize the store
$creator->getSession()->setCustomerId($customer->getId());
// Set active customer in session

$quote = Mage::getModel('sales/quote')->assignCustomer($customer);
$quote->setStore($customer->getStore());
$creator->setQuote($quote); // This was key to not duplicating the order contents

That's probably overkill, but it worked for me.

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.