Magento import csv files to database scripts

Source: Internet
Author: User
Magento import csv files to the database. we will use a plug-in named SplFileObject to import large amounts of data. of course, we can write this file on our own, but not necessarily better than this one... magento import csv files to the database. we will use a plug-in named SplFileObject to import large amounts of data. of course, we can write this file on our own, but not necessarily better than this.

This is another way of writing magento scripts. I personally think it is okay to write the script. I don't want to laugh at it. it is highly efficient to use SplFileObject to handle csv files of big data. the code is as follows:

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


Address:

Reprinted at will, but please attach the article address :-)

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.