0

I am creating 2 orders from admin session programmatically..Both these orders are of different customers.When I am execute script that time 2nd order is assigning to 1st order’s customer.

Address,products, all are assigning correctly in 2nd order.But only customer name which is of 1st order is set to wrong.

Please help me for this.

Below is my code for create order.

Test.php

Mynamespace_Mymodule_Model_Test
{
   public function createOrder()
   {
         foreach ($this->getItems() as $Item) {
        $order = Mage::getModel('sales/order')->load($Item->getPrimaryOrderId());
        break;
        }
    $items = $this->getItems()->getOrderItems();
    $order->setReordered(true);
    $quote = $this->getQuote()->setUpdatedAt(now());

    $quoteCurrency = Mage::getModel('directory/currency')->load($quote->getQuoteCurrencyCode());
    $quote->setForcedCurrency($quoteCurrency);
    $quote->save();
    $no = Mage::getSingleton('mymodule/order_create');
    $no
            ->reset()
            ->setRecollect(1)
            ->setPrimaryQuote($quote);

    $no->getSession()->setUseOldShippingMethod(true);
    $no->setSendConfirmation(true);
    $no->setData('account', $this->getCustomer()->getData());
    foreach ($this->getItems() as $Item) {
        $arr[] = $Item->getPrimaryOrderItemId();
    }
    $no->setItemIdFilter($arr);
    // }
    $no->initFromOrder($order);
    //fix for guest subscription
    $customer = $this->getGuestByEmail($order->getCustomerEmail());
    if (!$order->getCustomerId()) {
        if (!$customer->getId()) {
            $no->getQuote()->getCustomer()
                                 ->setEmail($order->getCustomerEmail())
                                 ->setFirstname($order->getCustomerFirstname())
                                 ->setLastname($order->getCustomerLastname());
        }
        elseif (!$order->getCustomerId())
            $no->getQuote()->setCustomer($customer);
    }
    else
        $no->getQuote()->setCustomer($customer);


    $or = $no->createOrder();

    $or->save();
    $no->getQuote()->removeAllItems();
    //clean base from fake guest records
    if (!$order->getCustomerId() && !$customer->getId()) {
        Mage::register('isSecureArea', true);
        $this->deleteFakeGuestsByEmail($order->getCustomerEmail());
    }

    return $or;
}

Create.php

class Mynamespace_Mymodule_Model_Order_Create extends Mage_Adminhtml_Model_Sales_Order_Create
{
  public function reset()
  {
    $this->_session->clear();
    $this->_session->setQuote(null);
    return $this;
  }

/**
 * Initialize creation data from existing order
 *
 * @param Mage_Sales_Model_Order $order
 * @return unknown
 */
public function initFromOrder(Mage_Sales_Model_Order $order)
{
    if (!$order->getReordered()) {
        $this->getSession()->setOrderId($order->getId());
    } else {
        $this->getSession()->setReordered($order->getId());
    }

    /**
     * Check if we edit quest order
     */
    $this->getSession()->setCurrencyId($order->getOrderCurrencyCode());
    if ($order->getCustomerId()) {
        $this->getSession()->setCustomerId($order->getCustomerId());
    } else {
        $this->getSession()->setCustomerId(false);
    }

    $this->getSession()->setStoreId($order->getStoreId());
    $this->getSession()->getStore();//need for initializing store

    $orderItems = Mage::getModel("sales/order")->load($order->getId()); //load order by order id

    foreach($orderItems->getAllVisibleItems() as $orderItem)
    {

        if (is_array($this->getItemIdFilter()) && sizeof($this->getItemIdFilter())) {
            // If itemId filter is set - ignore not matching entries
            if (array_search($orderItem->getId(), $this->getItemIdFilter()) === false) {
                continue;
            }
        }

        if (!$orderItem->getParentItem()) {
            if ($order->getReordered()) {
                $qty = $orderItem->getQtyOrdered();
            }
            else {
                $qty = $orderItem->getQtyOrdered() - $orderItem->getQtyShipped() - $orderItem->getQtyInvoiced();
            }

            if ($qty > 0) {

                $item = $this->initFromOrderItem($orderItem, $qty , $product->getPrice() );
                if (is_string($item)) {
                    Mage::throwException($item);
                }
            }
        }
    }
    $this->_initBillingAddressFromOrder($order);
    $this->_initShippingAddressFromOrder($order);


    $this->setShippingMethod($order->getShippingMethod());
    $this->getQuote()->getShippingAddress()->setShippingDescription($order->getShippingDescription());
    $this->getQuote()->getBillingAddress()->setShippingDescription($order->getShippingDescription());


    $this->getQuote()->getPayment()->addData($order->getPayment()->getData());

    $orderCouponCode = $order->getCouponCode();
    if ($orderCouponCode) {
        $this->getQuote()->setCouponCode($orderCouponCode);
    }

    if ($this->getQuote()->getCouponCode()) {
    }

    Mage::helper('core')->copyFieldset(
        'sales_copy_order',
        'to_edit',
        $order,
        $this->getQuote()
    );

    Mage::dispatchEvent('sales_convert_order_to_quote', array(
                                                             'order' => $order,
                                                             'quote' => $this->getQuote()
                                                        ));

    if (!$order->getCustomerId()) {
        $this->getQuote()->setCustomerIsGuest(true);
    }

    if ($this->getSession()->getUseOldShippingMethod(true)) {
        /*
         * if we are making reorder or editing old order
         * we need to show old shipping as preselected
         * so for this we need to collect shipping rates
         */
        $this->collectShippingRates();
    } else {

        /*
         * if we are creating new order then we don't need to collect
         * shipping rates before customer hit appropriate button
         */
        $this->collectRates();
    }
    $this->saveQuote();
    // Make collect rates when user click "Get shipping methods and rates" in order creating

    /** Check stock */
    foreach ($this->getQuote()->getAllVisibleItems() as $item) {

        $this->checkQuoteItemQty($item);
        if ($item->getProduct()->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_DISABLED) {
            $item->setMessage(Mage::helper('adminhtml')->__('This product is currently disabled'));
            $item->setHasError(true);
        }

        if ($item->getHasError()) {
                        }
    }


    $this->getQuote()->save();
    return $this;
  }
}

May be here total brackets are less.but that is not issue.Because in my file everything is ok.

4
  • post your code here – Magento 2 Feb 12 '16 at 4:33
  • Sorry but code is too long so i can not put that over here. – Charlotte Ac Feb 12 '16 at 6:28
  • But as I debug, I found that 2nd order is taking quote id of previous order. – Charlotte Ac Feb 12 '16 at 6:36
  • @Kothari Please check my updated quetion. – Charlotte Ac Feb 15 '16 at 12:14
0

Possibly the customer name variable you are assigning at end of your loop.. So when the loop runs for the second time , it still has the customer name from previous loop iteration .. Try initializing the customer name at the beginning of the loop If that doesnt work , then please try posting that part of the code here ..

1
  • Please check my updated quetion. – Charlotte Ac Feb 15 '16 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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