这是magento脚本的另外一种写法,个人觉得写的还行,高手莫见笑,用splfileobject来处理大数据的csv文件效率高,代码如下:
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 (emptyempty($this->_files)) { die(self::usagehelp()); } } // shell script point of entry public function run() { self::getdatafromcsv(); if (emptyempty($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 (emptyempty(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 (emptyempty($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' => $customerdata['lastname'], 'customer_mage_id' => $customer->getid() , 'passwd' => $customerdata['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' => $customerdata['lastname'], 'street' => $customerdata['billing_address'], 'company' => $customerdata['company'], 'city' => $customerdata['billing_city'], 'region_id' => '', 'region' => '', 'postcode' => $customerdata['billing_postcode'], 'country_id' => 'fi', 'telephone' => $customerdata['phone'], ); $customaddress = mage::getmodel('customer/address'); //$customaddress = new mage_customer_model_address(); $customaddress->setdata($same_address)->setcustomerid($customer->getid())->setisdefaultbilling('1')->setisdefaultshipping('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' => $customerdata['lastname'], 'street' => $customerdata['billing_address'], 'company' => $customerdata['company'], 'city' => $customerdata['billing_city'], 'region_id' => '', 'region' => '', 'postcode' => $customerdata['billing_postcode'], 'country_id' => 'fi', 'telephone' => $customerdata['phone'], ); self::setbillingaddress($billing_address, $customer); $shipping_address = array( 'firstname' => $customerdata['firstname'], 'lastname' => $customerdata['lastname'], 'street' => $customerdata['shipping_address'], 'company' => $customerdata['company'], 'city' => $customerdata['shipping_city'], 'region_id' => '', 'region' => '', 'postcode' => $customerdata['shipping_postcode'], 'country_id' => 'fi', 'telephone' => $customerdata['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 ($defaultshippingid = $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 ($defaultshippingid = $customer->getdefaultshipping()) { $customeraddress->load($defaultshippingid); } else { $customeraddress->setcustomerid($customer->getid())->setisdefaultshipping('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 _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 <
有一些朋友使用的是excel文档, 这样这个程序就不可以使用了, 我们可以使用phpexcel插件来读取excel文件并写入到数据库中去 .
本文地址:
转载随意,但请附上文章地址:-)