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

Popular posts from this blog

magento 1 extension converted in to magento 2 process

Simple Install =================================== Download as a zip below link. unzip and upload it to your public_html or other rootdoc directory. https://github.com/magento/code-migration. login to your server via shell switch to the code-migration-develop directory by running following command   cd code-migration-develop Run command following command in root directory of this toolkit composer instal Before running the migration, the following directories need to be prepared: ===================================== * `<src>` - Directory that contains custom Magento 1.x code that is intended to be migrated. The code must follow the Magento 1.x directory structure.   Magento1.x core files must not be included. * `<dst>` - Empty directory for the toolkit to put the generated Magento 2 code to * `<m1>` - Directory that contains: * Vanilla Magento 1.x codebase, and * Custom Magento 1.x code same as...

Magento 2 product collection Filtering multi-select attribute values

  If you have multi-select attribute of product like below If you want filter value for this option Use below syntax to get product data: ->addAttributeToFilter('store_model', array('finset' => $params['store_model'])) finset key is used for multiselect attribute filter. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $products = $objectManager->get('Magento\Catalog\Model\Product')         ->getCollection()         ->addAttributeToSelect('*')         ->addAttributeToSelect('store_brand')         ->addAttributeToSelect('store_model')         ->addAttributeToSelect('store_year')         ->addAttributeToFilter('store_brand', array('finset' => $params['store_brand']))         ->addAttributeToFilter('store_model', array('finset' => $params['store_model']))         ->ad...

Magento Debug HEADERS ALREADY SENT error

When you  receiving the following error in  system.log file: app\code\core\Mage\Core\Controller\Response\Http.php:44  [1] \www.pawan.com\lib\Zend\Controller\Response\Abstract.php:727  [2]  \www.pawan.com\app\code\core\Mage\Core\Controller\Response\Http.php:75  [3]  \www.pawan.com\app\code\core\Mage\Core\Controller\Varien\Front.php:188  [4]  \www.pawan.com\app\code\core\Mage\Core\Model\App.php:304  [5]  \www.pawan.com\app\Mage.php:596  [6]  \www.pawan.com\index.php:81 if you have no idea what file is causing this. Here is the steps to debug. That error is thrown from Mage_Core_Controller_Response_Http -> sendHeaders(). This function calls the super class function that actually does the check to see whether or not headers have already been sent, Zend_Controller_Response_Abstract -> canSendHeaders(). The Zend_Controller_Response_Abstract class handles, among other things, sending response header...