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...

What startSetup() and endSetup() methods actually does?

The startSetup() and endSetup() methods are used in setup scripts. They are often at the beginning and the end of an upgrade/install method, like in “upgrade()” method of Magento/Catalog/Setup/UpgradeData.php The question is “do you really need them?” Or does it just “look necessary”? This blog post explores what these functions do and then explains when you do and do not need to use these methods. Let’s see what these methods are doing. First, startSetup(): public function startSetup() {   $this->rawQuery("SET SQL_MODE=''");  $this->rawQuery("SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0");   $this->rawQuery("SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'");   return $this;  } 1. Disable foreign keys check. It may be necessary in some rare cases (for example, in a case of cyclic references between tables), but it’s not needed in common situations. It even may lead to hiding real problems...

Magento 2 UI Component Grid Explanation

1) layout file inside Company/Module/view/adminhtml/layout/routerid_controller_action.xml define grid as uiComponent with: 2) uiComponent is defined in Company/Module/view/adminhtml/ui_component/listing_name.xml file. File name must be the same as uiComponent name used in layout file. The structure of the file may seem pretty complex at first sight but as always these are some repeating nodes. To make it simple lets slice it. Main node of the component file is <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">. It is fixed and I believe it requires namespace location attribute. Next there are typically 4 nodes inside <listing /> node: <argument />, <dataSource />, <container /> and <columns />. This is however not a strict setup as <argument /> node might be duplicated to provide more configuration or <container /> as...