Magento import csv files to database scripts

Source: Internet
Author: User
Tags foreach explode getmessage sprintf trim

This is another way of writing magento scripts.

I personally think that writing is okay, and the experts don't laugh.

Using SplFileObject to handle big data csv files is highly efficient

 

The code is as follows: Copy code
<? Php
Require_once 'shell/abstract. Php ';
Class Faarao_Import_Customer extends Mage_Shell_Abstract
{
Protected $ _ files = array ();
Protected $ _ datas = array ();
Public function _ construct (){
Parent: :__ construct ();
Set_time_limit (0 );
If ($ this-> getArg ('file ')){
$ This-> _ files = array_merge (
$ This-> _ files,
Array_map (
'Trim ',
Explode (',', $ this-> getArg ('file '))
)
);
Foreach ($ this-> _ files as $ key => $ file ){
$ Extension = self: get_extension ($ file );
If ($ extension! = 'Csv '){
Unset ($ this-> _ files [$ key]);
}
}
}
If (empty ($ this-> _ files )){
Die (self: usageHelp ());
}
}
// Shell script point of entry
Public function run (){
Self: getDataFromCsv ();
If (empty ($ this-> _ datas )){
Die ("not found data in csv! \ R \ n ");
}
$ EmailBooks = array ();
Foreach ($ this-> _ datas as $ name => $ datas ){
Echo "filename: {$ name }=================================\ r \ n ";
$ ImportNum = 0;
Foreach ($ datas as $ key => $ data ){
If (empty (trim ($ data [4]) {
Continue;
}
$ CustomerData ['firstname'] = trim ($ data [0]);
$ CustomerData ['lastname'] = trim ($ data [1]);
$ CustomerData ['phone'] = trim ($ data [2]);
$ CustomerData ['mobile'] = trim ($ data [3]);
$ CustomerData ['email '] = trim ($ data [4]);
$ CustomerData ['Company'] = trim ($ data [5]);
$ CustomerData ['billing _ address'] = $ data [6]. $ data [7]. $ data [8];
$ CustomerData ['billing _ postcode'] = sprintf ("% 05d", trim ($ data [9]);
$ CustomerData ['billing _ City'] = trim ($ data [10]);
$ CustomerData ['billing _ country '] = trim ($ data [11]);
$ CustomerData ['shipping _ address'] = $ data [12]. $ data [13]. $ data [14];
If (empty ($ data [15]) & is_numeric ($ data [16]) {
$ CustomerData ['shipping _ postcode'] = sprintf ("% 05d", trim ($ data [16]);
$ CustomerData ['shipping _ City'] = trim ($ data [17]);
$ CustomerData ['shipping _ country'] = trim ($ data [18]);
} Else {
$ CustomerData ['shipping _ postcode'] = sprintf ("% 05d", trim ($ data [15]);
$ CustomerData ['shipping _ City'] = trim ($ data [16]);
$ CustomerData ['shipping _ country'] = trim ($ data [17]);
}
// $ CustomerData ['email '] = 'zouhongzhao @ 126.com ';
$ CustomerData ['country'] = 'finland ';
$ CustomerData ['password'] = self: randomkeys (10 );
 
& Nbsp;
 
Echo "customer email {$ customerData ['email ']}... \ r \ n ";
Print_r ($ customerData );
$ Customer = Mage: getModel ('customer/customer ');
$ Customer-> setWebsiteId (Mage: app ()-> getWebsite ()-> getId ());
$ Customer-> loadByEmail ($ customerData ['email ']);
If (! $ Customer-> getId ()){
Echo "insert... \ r \ n ";
$ Customer-> setEmail ($ customerData ['email ']);
$ Customer-> setFirstname ($ customerData ['firstname']);
$ Customer-> setLastname ($ customerData ['lastname']);
$ Customer-> setPassword ($ customerData ['password']);
} Else {
Echo "update... \ r \ n ";
}
Try {
$ Customer-> save ();
$ Customer-> setConfirmation (null );
$ Customer-> save ();
// Make a "login" of new customer
Mage: getSingleton ('customer/session')-> loginById ($ customer-> getId ());
$ ImportNum ++;
$ EmailBooks [$ customerData ['email '] = array (
'Firstname' => $ customerData ['firstname'],
'Lastname' => $ mermerdata ['lastname'],
'Customer _ mage_id '=> $ customer-> getId (),
'Passwd' => $ mermerdata ['password']
);
 
Echo "customer save OK! \ R \ n ";
}
Catch (Exception $ ex ){
Echo "customer save fail! \ R \ n ";
Continue;
}
 
If (trim ($ customerData ['billing _ address']) = trim ($ customerData ['shipping _ address'])
& $ CustomerData ['billing _ postcode'] = $ customerData ['shipping _ postcode']
& $ CustomerData ['billing _ City'] = $ customerData ['shipping _ City']) {
$ Same_address = array (
'Firstname' => $ customerData ['firstname'],
'Lastname' => $ mermerdata ['lastname'],
'Street '=> $ mermerdata ['billing _ address'],
'Company' => $ mermerdata ['Company'],
'City' => $ mermerdata ['billing _ City'],
'Region _ id' => '',
'Region' => '',
'Postcode' => $ mermerdata ['billing _ postcode'],
'Country _ id' => 'Fi ',
'Telphone' => $ mermerdata ['phone'],
);
 
$ CustomAddress = Mage: getModel ('customer/address ');
// $ CustomAddress = new Mage_Customer_Model_Address ();
$ CustomAddress-> setData ($ same_address)
-> SetCustomerId ($ customer-> getId ())
-> SetIsDefaultBilling ('1 ')
-> Setisdefashipshipping ('1 ')
-> SetSaveInAddressBook ('1 ');
Try {
$ CustomAddress-> save ();
Echo "sameAddress save OK! \ R \ n ";
}
Catch (Exception $ ex ){
Echo "sameAddress save fail! \ R \ n ";
Continue;
}
} Else {
$ Billing_address = array (
'Firstname' => $ customerData ['firstname'],
'Lastname' => $ mermerdata ['lastname'],
'Street '=> $ mermerdata ['billing _ address'],
'Company' => $ mermerdata ['Company'],
'City' => $ mermerdata ['billing _ City'],
'Region _ id' => '',
'Region' => '',
'Postcode' => $ mermerdata ['billing _ postcode'],
'Country _ id' => 'Fi ',
'Telphone' => $ mermerdata ['phone'],
 
);
Self: setBillingAddress ($ billing_address, $ customer );
$ Shipping_address = array (
'Firstname' => $ customerData ['firstname'],
'Lastname' => $ mermerdata ['lastname'],
'Street '=> $ mermerdata ['shipping _ address'],
'Company' => $ mermerdata ['Company'],
'City' => $ mermerdata ['shipping _ City'],
'Region _ id' => '',
'Region' => '',
'Postcode' => $ mermerdata ['shipping _ postcode'],
'Country _ id' => 'Fi ',
'Telphone' => $ mermerdata ['phone'],
);
Self: setShippingAddress ($ shipping_address, $ customer );
}
// Die;
}
Echo "import num: {$ importNum }===================================\ r \ n ";
}
// Save passwd
$ Fp = fopen ('customer _ record. Log', 'w ');
Fwrite ($ fp, json_encode ($ emailBooks ));
Fclose ($ fp );
 
}
 
Public function setBillingAddress ($ data, $ customer ){
$ CustomerAddress = Mage: getModel ('customer/address ');
 
If ($ defashipshippingid = $ customer-> getDefaultBilling ()){
$ CustomerAddress-> load ($ defaultShippingId );
} Else {
$ CustomerAddress
-> SetCustomerId ($ customer-> getId ())
-> SetIsDefaultBilling ('1 ')
-> SetSaveInAddressBook ('1 ')
;
 
$ Customer-> addAddress ($ customerAddress );
}
 
Try {
$ CustomerAddress
-> AddData ($ data)
-> Save ()
;
Echo "BillingAddress save OK! \ R \ n ";
} Catch (Exception $ e ){
// Mage: log ('address Save Error: '. $ e-> getMessage ());
Echo "BillingAddress save fail! \ R \ n ";
}
}
 
Public function setShippingAddress ($ data, $ customer ){
$ CustomerAddress = Mage: getModel ('customer/address ');
 
If ($ defashipshippingid = $ customer-> getdefashipshipping ()){
$ CustomerAddress-> load ($ defaultShippingId );
} Else {
$ CustomerAddress
-> SetCustomerId ($ customer-> getId ())
-> Setisdefashipshipping ('1 ')
-> SetSaveInAddressBook ('1 ')
;
 
$ Customer-> addAddress ($ customerAddress );
}
 
Try {
$ CustomerAddress
-> AddData ($ data)
-> Save ()
;
Echo "ShippingAddress save OK! \ R \ n ";
} Catch (Exception $ e ){
// Mage: log ('address Save Error: '. $ e-> getMessage ());
Echo "ShippingAddress save fail! \ R \ n ";
}
}
 
Public function randomkeys ($ length ){
$ ReturnStr = '';
$ Pattern = '1234567890abcdefghijklmnopqrstuvwxyzabcdefghijklomnopqrstuvwxyz ';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ ReturnStr. = $ pattern {mt_rand (0, 61 )};
}
Return $ returnStr;
}
Public function getDataFromCsv (){
$ This-> _ files = array_unique ($ this-> _ files );
$ CsvCustomers = array ();
Foreach ($ this-> _ files as $ filename ){
Echo "current file: {$ filename} \ r \ n ";
Setlocale (LC_ALL, 'en _ US.UTF-8 ');
$ Content = file_get_contents ($ filename );
$ Data = mb_detect_encoding () ($ content, 'utf-8', true );
// $ Data = iconv ("CP1257", "UTF-8", $ content );
File_put_contents ($ filename, $ content );
 
// Fclose ($ handle );
// Print_r ($ content); die;
$ Basename = basename ($ filename, ". csv ");
$ Data = array ();
$ Tmp = array ();
$ Spl_object = new SplFileObject ($ filename, 'RB ');
$ Spl_object-> seek (filesize ($ filename ));
$ Start = 0;
$ Num = $ spl_object-> key ();
$ Spl_object-> seek ($ start );
While ($ num --&&! $ Spl_object-> eof ()){
$ Data [] = $ spl_object-> fgetcsv ();
$ Spl_object-> next ();
}
Foreach ($ data as $ key => $ values ){
If ($ key = 0 ){
Continue;
}
$ MergeValue = explode (';', implode (';', $ values ));
// If (count ($ mergeValue )! = 20 ){
// Continue;
//}
Array_push ($ tmp, $ mergeValue );
}
 
$ This-> _ datas [$ basename] = $ tmp;
}
Return $ this;
}
 
Public function get_extension ($ filename ){
Return pathinfo ($ filename, PATHINFO_EXTENSION );
}
// Usage instructions
Public function usageHelp ()
{
Return <USAGE
Usage: php tetuan_customer_import.php -- file a.csv, B .csv
\ N
USAGE;
}
}
// Instantiate
$ Shell = new Faarao_Import_Customer ();
// Initiate script
$ Shell-> run ();

Supplement: some friends use excel documents, so this program cannot be used. We can use the phpexcel plug-in to read excel files and write them into the database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.