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 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 2 best seller product display based on current category and subcategories

Magento 2 getting best seller based on category wise displaying i have done below for that to implement this functionality it is working fine for me. step 1 create block file in our module folder <?php namespace Pawan\Bestseller\Block; use Magento\Catalog\Api\CategoryRepositoryInterface; class Bestsellercategory extends \Magento\Catalog\Block\Product\ListProduct {     /**      * Product collection model      *      * @var Magento\Catalog\Model\Resource\Product\Collection      */     protected $_collection;     /**      * Product collection model      *      * @var Magento\Catalog\Model\Resource\Product\Collection      */     protected $_productCollection;     /**      * Image helper      *      * @var Magento\Catalog\Helper\Image     ...

order cancel from front end

if you want cancel order from front end create module like this app/etc/modules/<Namespace_Modulename>.xml <config>     <modules>         <Namespace_CustomerOrderCancel>             <active>true</active>             <codePool>community</codePool>             <depends>                 <Mage_Sales />             </depends>         </ Namespace _CustomerOrderCancel>     </modules> </config> app/community/Namespace/Modulename           ------controllers           -------etc           -------Helper if you want cancel button in history page (myorder) need override block file Mage_Sales_Block_Orde...