Skip to main content

Move users from Zen Cart to Magento

This python script will connect to a mysql database and grab all the existing users information from Zen Cart and create a Magento import csv file.

text/x-python create_users_export.py — 2 KB

File contents

import MySQLdb

def connect_to_mysql():
    db = 'zen'
    host = 'localhost'
    user = 'root'
    password = ''
    return MySQLdb.Connection(db=db, host=host, user=user, passwd=password)

def get_users():
    column_list = ['firstname', 'lastname', 'email', 'billing_street_full', 'billing_postcode', 'billing_city']
    def package_results(res):
        new_results = []
        
        for product in res:
            new = {}
            
            for i in range(0, len(column_list)):
                new[column_list[i]] = product[i]
                
            new_results.append(new)
    
        return new_results
    
    conn = connect_to_mysql()
    
    cursor = conn.cursor()
    sql = """
        SELECT  customers.customers_firstname as firstname,
                        customers.customers_lastname as lastname,
                        customers.customers_email_address as email,
                        addresses.entry_street_address as billing_street_full,
                        addresses.entry_postcode as billing_postcode,
                        addresses.entry_city as billing_city
                FROM zen.customers customers
                INNER JOIN zen.address_book addresses 
                ON customers.customers_default_address_id = addresses.address_book_id
    """
    
    cursor.execute(sql)
    results = cursor.fetchall()
    cursor.close()
    conn.close()
    return package_results(results)
    
    
csv_columns = {
    'website' : 'base',
    'group_id' : 'General',
    'created_in' : 'default',
    'is_subscribed' : 0
}
csv_columns_ordered_list = [
    'website',
    'email',
    'group_id',
    'firstname',
    'lastname',
    'password_hash',
    'prefix',
    'middlename',
    'suffix',
    'taxvat',
    'billing_prefix',
    'billing_firstname',
    'billing_middlename',
    'billing_lastname',
    'billing_suffix',
    'billing_street_full',
    'billing_city',
    'billing_region', 
    'billing_country',
    'billing_postcode',	
    'billing_telephone',
    'billing_company',	
    'billing_fax',
    'shipping_prefix',	
    'shipping_firstname',
    'shipping_middlename',
    'shipping_lastname',
    'shipping_suffix',
    'shipping_street_full',
    'shipping_city',
    'shipping_region',
    'shipping_country',
    'shipping_postcode',
    'shipping_telephone',
    'shipping_company',
    'shipping_fax',
    'created_in',
    'is_subscribed'
]
    
import csv
users = get_users()
csv_file = csv.writer(open('import_users.csv', 'w'), delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)

csv_file.writerow(csv_columns_ordered_list)

for user in users:
    row = {}
    for key in csv_columns_ordered_list:
        row[key] = ''
        
    #set defaults
    for key, value in csv_columns.items():
        row[key] = value
    
    for column, value in user.items():
        row[column] = value
    
    writable_row = []
    for key in csv_columns_ordered_list:
        writable_row.append(row[key])
 
    csv_file.writerow(writable_row)