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 — 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]
        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
    results = cursor.fetchall()
    return package_results(results)
csv_columns = {
    'website' : 'base',
    'group_id' : 'General',
    'created_in' : 'default',
    'is_subscribed' : 0
csv_columns_ordered_list = [
import csv
users = get_users()
csv_file = csv.writer(open('import_users.csv', 'w'), delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)


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: