Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Creates an import file for WooCommerce Memberships from an s2Member export
// https://docs.woocommerce.com/document/woocommerce-memberships-import-and-export/
// Intall node.js then run the following commands in a new directory containing this file
// npm init
// npm install --save csv-parser csv-writer moment
// the following line runs the file. Be sure to change the constants below first
// node --inspect s2MemberToWooCommerceMemberships.js
const csv = require('csv-parser')
const fs = require('fs')
const moment = require('moment')
const csvwriter = require('csv-writer')
// Change these
const membership_plan_id = '12345' // The membership ID for the membership you've created in WooCommerce Memberships
const membership_plan_slug = 'membershipslug' // The membership slug for the membership you've created in WooCommerce Memberships
const membership_status = "active" // The WooCommerce membership status of the imported users
const s2role = 's2member_level1' // The existing s2Member role to import -- match the role in the exported CSV file
const exportCSV = 'export.csv' // The name of the advanced export file from s2Member. Must be in the same directory as this file
const importCSV = 'import.csv' // The name of the new file to import to WooCommerce Memberships
const results = []
fs.createReadStream( exportCSV )
.pipe(csv())
.on('data', data => {
if ( data['role'] === s2role && data['meta_key__wp_s2member_auto_eot_time']) { // Skips any memberships you manually extended to clean things up
const newrow = {};
const eot = new Date(data['meta_key__wp_s2member_auto_eot_time']*1000)
const expiration = moment(eot).format('YYYY-MM-DD hh:mm:ss')
const paid = new Date(data['meta_key__wp_s2member_last_payment_time']*1000)
const last_payment = moment(paid).format('YYYY-MM-DD hh:mm:ss')
newrow.user_membership_id = ''
newrow.user_id = data['ID']
newrow.user_name = data['user_login']
newrow.member_email = data['user_email']
newrow.member_first_name = data['meta_key__first_name']
newrow.member_last_name = data['meta_key__last_name']
newrow.membership_plan_id = membership_plan_id
newrow.membership_plan_slug = membership_plan_slug
newrow.membership_status = membership_status
newrow.subscription_id = ''
newrow.member_since = last_payment
newrow.membership_expiration = expiration
results.push(newrow)
}
})
.on('end', () => {
const csvWriter = csvwriter.createObjectCsvWriter({
path: importCSV,
header: [
{id: 'user_membership_id', title: 'user_membership_id'},
{id: 'user_id', title: 'user_id'},
{id: 'user_name', title: 'user_name'},
{id: 'member_email', title: 'member_email'},
{id: 'member_first_name', title: 'member_first_name'},
{id: 'member_last_name', title: 'member_last_name'},
{id: 'membership_plan_id', title: 'membership_plan_id'},
{id: 'membership_plan_slug', title: 'membership_plan_slug'},
{id: 'membership_status', title: 'membership_status'},
{id: 'subscription_id', title: 'subscription_id'},
{id: 'member_since', title: 'member_since'},
{id: 'membership_expiration', title: 'membership_expiration'}
]
})
csvWriter.writeRecords(results)
.then( () => console.log('The CSV file was written successfully') )
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment