Skip to main content

Orders Import using csv and update existing orders

Ever wondered how to import bulk orders programatically? I want import orders using .csv i tried so many sites but i unable get anything related to import using csv i got it by creating programatically. So i tried csv bulk import
orders because i have 60K orders so order creation is difficult i have orders data csv.So that i prepared csv and i wrote code related to orders import.Below is code 


Copy the below code and create one file in your magento root directory(magento/ordersimports.php)paste the code into the file ........

Next prepare accordingly ordersimports.csv this csv file also you can place root directory(magento/ordersimports.csv) 

after that go to your site in browser run file like this 
www.magento.com/ordersimports.php

<?php

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$arr = array();
if (($handle = fopen("ordersimports.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 60000, ",")) !== FALSE) {
        $incrementid = $data[0];
        $billingfname = $data[1];
        $billinglname = $data[2];
        $email = $data[3];
        $groupid = $data[4];
        $company = $data[7];
        $billingstreet = $data[8];
        $billingstreet1 = $data[9];
        $billingstreet2 = $data[10];
        //$arr[$data[5]]['billingstreet'] = $data[9];
        $billingcity = $data[11];
        $billingregion = $data[12];
        $billingzip = $data[13];
        $billingphone = $data[14];
        $billingcountry = $data[15];

        $shippingfname = $data[16];
        $shippinglname = $data[17];
        $shippingcompany = $data[18];
        $shippingstreet = $data[19];
        $shippingstreet1 = $data[20];
        $shippingstreet2 = $data[21];
        $shippingcity = $data[22];
        $shippingregion = $data[23];
        $shippinggzip = $data[24];
        $shippingcountry = $data[25];
        $shippingphone = $data[26];
        $shippingmethod = $data[27];
        $paymentmethod = $data[33];
        $paymentmethodref = $data[34];

        $ordersubtotal = $data[28];
        $ordertax = $data[29];
        $ordershippingcost = $data[30];
        $ordertotal = $data[31];
        $orderdate = $data[32];
        $qty = $data[38];
        $productname = $data[41];
        $sku = $data[42];
        $price = $data[43];
        $simplesku = $data[44];
        //echo $incrementid.'<br />';
        $customer = Mage::getModel("customer/customer")->setWebsiteId(Mage::app()->getWebsite()->getId())->loadByEmail($email);
        
        $customerId = $customer->getId();
        $transaction = Mage::getModel('core/resource_transaction');
        $orderdata = Mage::getModel('sales/order')->loadByIncrementId($incrementid);
        //echo'<pre>';print_r($orderdata->getData());
        if ($incrementid != $orderdata->getIncrementId()) {
            echo 'if loki vachhanu';
            // set Billing Address
            $storeId = $customer->getStoreId();
            $billing = $customer->getDefaultBillingAddress();

            $billingAddress = Mage::getModel('sales/order_address')
                    ->setStoreId($storeId)
                    ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
                    ->setCustomerId($customerId)
                    ->setCustomerAddressId($customer->getDefaultBilling())
                    ->setCustomer_address_id($customer->getEntityId())
                    ->setFirstname($billingfname)
                    ->setLastname($billinglname)
                    ->setCompany($company)
                    ->setStreet($billingstreet2 . $billingstreet . $billingstreet1)
                    ->setCity($billingcity)
                    ->setCountry_id($billingcountry)
                    ->setRegion($billingregion)
                    ->setPostcode($billingzip)
                    ->setTelephone($billingphone);
//            $order->setBillingAddress($billingAddress);
            $shipping = $customer->getDefaultShippingAddress();
            $shippingAddress = Mage::getModel('sales/order_address')
                    ->setStoreId($storeId)
                    ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
                    ->setCustomerId($customerId)
                    ->setCustomerAddressId($customer->getDefaultShipping())
                    ->setCustomer_address_id($customer->getEntityId())
                    ->setFirstname($shippingfname)
                    ->setLastname($shippinglname)
                    ->setCompany($shippingcompany)
                    ->setStreet($shippingstreet2 . $shippingstreet . $shippingstreet1)
                    ->setCity($shippingcity)
                    ->setCountry_id($shippingcountry)
                    ->setRegion($shippingregion)
                    ->setPostcode($shippinggzip)
                    ->setTelephone($shippingphone);

            // $order->setShippingAddress($shippingAddress)
            // ->setShippingMethod('flatrate_flatrate')
            // ->setShippingDescription('flatrate');


            $order = Mage::getModel('sales/order')
                    ->setIncrementId($incrementid)
                    ->setStoreId($storeId)
                    ->setQuoteId(0)
                    ->setGlobal_currency_code('USD')
                    ->setBase_currency_code('USD')
                    ->setStore_currency_code('USD')
                    ->setOrder_currency_code('USD')
                    ->setCustomer_email($email)
                    ->setCustomerFirstname($billingfname)
                    ->setCustomerLastname($billinglname)
                    ->setCustomerGroupId($groupid)
                    ->setCustomer_is_guest(0)
                    ->setCustomer($customer)
                    ->setBillingAddress($billingAddress)
                    ->setShippingAddress($shippingAddress)
                    ->setShippingMethod('flatrate_flatrate')
                    ->setShippingDescription('flatrate');
//                    ->setPayment($orderPayment);
            $orderPayment = Mage::getModel('sales/order_payment')
                    ->setStoreId($storeId)
                    ->setCustomerPaymentId(0)
                    ->setMethod('cashondelivery');
            $order->setPayment($orderPayment);
            $product_id = Mage::getModel("catalog/product")->getIdBySku($simplesku);
            $_product = Mage::getModel('catalog/product')->load($product_id);
            $rowTotal = $price * $qty;
            $orderItem = Mage::getModel('sales/order_item')->setStoreId($storeId)
                    ->setQuoteItemId(0)
                    ->setQuoteParentItemId(NULL)
                    ->setProductId($product_id)
                    ->setProductType($_product->getTypeId())
                    ->setQtyBackordered(NULL)
                    ->setTotalQtyOrdered($qty)
                    ->setQtyOrdered($qty)
                    ->setName($productname)
                    ->setSku($simplesku)
                    ->setPrice($price)
                    ->setBasePrice($price)
                    ->setOriginalPrice($price)
                    ->setRowTotal($rowTotal)
                    ->setBaseRowTotal($rowTotal);
            $subTotal += $rowTotal;

            $order->addItem($orderItem);
            $order->setSubtotal($ordersubtotal)
                    ->setBaseSubtotal($ordersubtotal)
                    ->setTaxAmount($ordertax)
                    ->setBaseTaxAmount($ordertax)
                    ->setBaseShippingAmount($shippingdi)
                    ->setShippingAmount($shippingdi)
                    ->setGrandTotal($ordertotal)
                    ->setBaseGrandTotal($ordertotal);
            $history = $order->addStatusHistoryComment($shippingmethod . '<br/>' . $paymentmethod . '<br/>' . $paymentmethodref . '<br/>' . $ordershippingcost, false)
                    ->setIsCustomerNotified(false);
            $transaction->addObject($order);
            $transaction->addCommitCallback(array($order, 'place'));
            $transaction->addCommitCallback(array($order, 'save'));

            $transaction->save();
            $last_order_increment_id = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
            echo $last_order_increment_id;
            echo "<br/>";
        } else {
            echo 'else loki vachhanu-------------------------';
            $product_id = Mage::getModel("catalog/product")->getIdBySku($simplesku);
            $_product = Mage::getModel('catalog/product')->load($product_id);
            $rowTotal = $price * $qty;
            $orderItem = Mage::getModel('sales/order_item')->setStoreId($storeId)
                     ->setStoreId($orderdata->getStore()->getStoreId())
                    ->setQuoteItemId(0)
                    ->setQuoteParentItemId(NULL)
                    ->setProductId($product_id)
                    ->setProductType($_product->getTypeId())
                    ->setQtyBackordered(NULL)
                    ->setTotalQtyOrdered($qty)
                    ->setQtyOrdered($qty)
                    ->setName($productname)
                    ->setSku($simplesku)
                    ->setPrice($price)
                    ->setBasePrice($price)
                    ->setOriginalPrice($price)
                    ->setRowTotal($rowTotal)
                    ->setBaseRowTotal($rowTotal)
                    ->setOrder($orderdata);
//            $orderdata->addItem($orderItem);
            $orderItem->save();
            $last_order_increment_id = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
            echo $last_order_increment_id;
            echo "<br/>";
        }
    }

}

if condition contain if order id not exists it will create new order.Orther wise add item to the existing order in else condition.

Keep trak if any issues

Comments